python stringvar函数_python-Tkinter StringVar错误

嗨,我收到此代码未定义StringVar()的错误,这可能是一件小事,但我对tkinter并不了解,希望获得一些帮助,谢谢.

这是我的代码:

import tkinter as tk

class Converter1(tk.Tk):

def __init__(self):

tk.Tk.__init__(self)

self.entry = tk.Entry(self)

self.variable = StringVar()

self.variable.set("Miles to Kilometers") # default dropdown menu value

self.menu = tk.OptionMenu(self, variable, "Miles to Kilometers", "Kilometers to Miles")

self.button = tk.Button(self, text="Convert!", command=self.convertMK)

self.button.pack()

self.menu.pack()

self.button.pack()

self.entry.pack()

def convtertMK(self): # converts the miles and kilometers using the dropdown menu

if var.get() == "Miles to Kilometers":

print(int(self.entry.get()) * 1.6093)

else:

print(int(self.entry.get()) / 1.6093)

converter = Converter1()

这是错误:

Traceback (most recent call last):

File "/Users/MaxBookPro/Desktop/test.py", line 25, in

converter = Converter1()

File "/Users/MaxBookPro/Desktop/test.py", line 8, in __init__

self.variable = Variable1

NameError: global name 'Variable1' is not defined

再次感谢.

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页