马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想请问一下大家为什么我这个程序运行会显示AttributeError: Module "tkinter" has no attribute"Stringvar", 按理来说这个StringVar不就是tkinter模块下的属性吗?
而且我就是这个程序有问题,另外的用到stringvar的程序却没有这样报错。
下面的代码第一个是有问题的,第二个是没有问题的。import tkinter as tk
window = tk.Tk()
window.geometry("400x400")
var1 = tk.StringVar()
l = tk.Label(window, textvariable = var1, bg = "yellow")
l.pack()
def click():
value = lb.get(lb.curselection())
var1.set(value)
b = tk.Button(window, text = "print", command = click)
b.pack()
var2 = tk.Stringvar()
var2.set(11,22,33,44)
lb = tk.Listbox(window, listvariable = var2 )
items = (a,b,c,d)
for item in items:
lb.insert("end", item)
lb.pack()
window.mainloop()
import tkinter as tk
#创建并设置主窗口
window = tk.Tk()
window.title("my window")
window.geometry("500x300")
#创建并设置label
var = tk.StringVar()
l = tk.Label(window, textvariable = var, bg = "red")
l.pack()
#创建button并使用
flag = False
def hit():
global flag
if not flag:
flag = True
var.set("you hit me")
else:
flag = False
var.set("")
b = tk.Button(window, text = "hit me", command = hit)
b.pack()
#主窗口循环
window.mainloop()
你写成了 Stringvar,看第 15 行:
import tkinter as tk
window = tk.Tk()
window.geometry("400x400")
var1 = tk.StringVar()
l = tk.Label(window, textvariable = var1, bg = "yellow")
l.pack()
def click():
value = lb.get(lb.curselection())
var1.set(value)
b = tk.Button(window, text = "print", command = click)
b.pack()
var2 = tk.StringVar() # 修改
var2.set(11,22,33,44)
lb = tk.Listbox(window, listvariable = var2 )
items = (a,b,c,d)
for item in items:
lb.insert("end", item)
lb.pack()
window.mainloop()
|