gpt4 book ai didi

python - 无法使用 set() 方法设置 tk.StringVar 的值

转载 作者:行者123 更新时间:2023-12-01 01:15:54 25 4
gpt4 key购买 nike

我正在尝试使用 tkinter 制作计算器的 GUI 应用程序。我创建了一个继承自 tkinter.Frame 的小部件类,其中有一个变量 display_string(tkinter.StringVar) ,其中包含要在计算器字符串上显示的字符串。我无法通过 set() 为该变量设置任何值。有人可以解释一下我出了什么问题吗?我在第 13 行收到以下错误。

TypeError: set() missing 1 required positional argument: 'value'

我按照 Alan D. Moore 的《Python - GUI programming tkinter》一书中的代码示例进行操作,效果很好。

引用

import tkinter as tk
from tkinter import ttk

class HelloView(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)

self.name = tk.StringVar()
self.hello_string = tk.StringVar()
#--------------------------------
self.hello_string.set("Hello World")
#-------------------------------------
name_label = ttk.Label(self, text="Name: ")
name_entry = ttk.Entry(self, textvariable=self.name)
change_button = ttk.Button(self, text="Change", command=self.on_change)
hello_label = ttk.Label(self, textvariable=self.hello_string,
font=("TkDefaultFont", 64), wraplength=600)
# Layout form
name_label.grid(row=0, column=0, sticky=tk.W)
name_entry.grid(row=0, column=1, sticky=(tk.W + tk.E))
change_button.grid(row=0, column=2, sticky=(tk.E))
hello_label.grid(row=1, column=0, columnspan=3)
self.columnconfigure(1, weight=1)

def on_change(self):
if self.name.get().strip():
self.hello_string.set("Hello " + self.name.get())
else:
self.hello_string.set("Hello World")

class MyApplication(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# set the window properties
self.title("Hello Tkinter")
self.geometry("800x300")
self.resizable(width=False, height=False)
# define the ui
HelloView(self).grid(sticky=(tk.E + tk.W + tk.N + tk.S))
self.columnconfigure(0, weight=1)

if __name__ == '__main__':
app = MyApplication()
app.mainloop()

我的代码

import tkinter as tk
from tkinter import ttk

class CalcView(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)

self.display_string = tk.StringVar
self.disp_ans = tk.IntVar
self.operand1 = tk.IntVar
self.operand2 = tk.IntVar
self.operation = self.output

# Here's the error:#---------------------------------------
self.display_string.set("Hey")
##-----------------------------------------------------------

display_label = ttk.Label(self, textvariable = self.display_string)

button1 = ttk.Button(self, text = '7', command = self.add_seven)
button2 = ttk.Button(self, text = '8', command = self.add_eight)
button3 = ttk.Button(self, text = '9', command = self.add_nine)
button4 = ttk.Button(self, text = '4', command = self.add_four)
button5 = ttk.Button(self, text = '5', command = self.add_five)
button6 = ttk.Button(self, text = '6', command = self.add_six)
button7 = ttk.Button(self, text = '1', command = self.add_one)
button8 = ttk.Button(self, text = '2', command = self.add_two)
button9 = ttk.Button(self, text = '3', command = self.add_three)
button10 = ttk.Button(self, text = '0', command = self.add_zero)

button11 = ttk.Button(self, text = 'C', command = self.clear)
button12 = ttk.Button(self, text = 'X', command = self.multiplication)
button13 = ttk.Button(self, text = '\\', command = self.division)
button14 = ttk.Button(self, text = '=', command = self.output)
button15 = ttk.Button(self, text = '+', command = self.addition)
button16 = ttk.Button(self, text = '-', command = self.subtraction)

display_label.grid(row = 0, columnspan = 4, sticky = (tk.E + tk.W))

button1.grid(row = 1, column = 0, sticky = (tk.E + tk.W))
button2.grid(row = 1, column = 1, sticky = (tk.E + tk.W))
button3.grid(row = 1, column = 2, sticky = (tk.E + tk.W))
button4.grid(row = 2, column = 0, sticky = (tk.E + tk.W))
button5.grid(row = 2, column = 1, sticky = (tk.E + tk.W))
button6.grid(row = 2, column = 2, sticky = (tk.E + tk.W))
button7.grid(row = 3, column = 0, sticky = (tk.E + tk.W))
button8.grid(row = 3, column = 1, sticky = (tk.E + tk.W))
button9.grid(row = 3, column = 2, sticky = (tk.E + tk.W))
button10.grid(row = 4, column = 0, sticky = (tk.E + tk.W))
button11.grid(row = 1, column = 3, sticky = (tk.E + tk.W))
button12.grid(row = 2, column = 3, sticky = (tk.E + tk.W))
button13.grid(row = 3, column = 3, sticky = (tk.E + tk.W))
button14.grid(row = 4, column = 3, sticky = (tk.E + tk.W))
button15.grid(row = 4, column = 2, sticky = (tk.E + tk.W))
button16.grid(row = 4, column = 1, sticky = (tk.E + tk.W))

self.columnconfigure(0, weight = 1)

def add_nine(self):
pass#self.display_string.set(self.display_string.get() + '9')

def add_eight(self):
pass#self.display_string.set(self.display_string.get() + '8')

def add_seven(self):
pass#self.display_string.set(self.display_string.get() + '7')

def add_six(self):
pass#self.display_string.set(self.display_string.get() + '6')

def add_five(self):
pass#self.display_string.set(self.display_string.get() + '5')

def add_four(self):
pass#self.display_string.set(self.display_string.get() + '4')

def add_three(self):
pass#self.display_string.set(self.display_string.get() + '3')

def add_two(self):
pass#self.display_string.set(self.display_string.get() + '2')

def add_one(self):
pass#self.display_string.set(self.display_string.get() + '1')

def add_zero(self):
pass#self.display_string.set(self.display_string.get() + '0')

def clear(self):
pass

def multiplication(self):
pass

def division(self):
pass

def addition(self):
pass

def subtraction(self):
pass

def output(self):
pass

class MainApplication(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.title("Calculator")
self.geometry("300x600")
self.resizable(width = False, height = False)

CalcView(self).grid(sticky = (tk.N + tk.S + tk.E + tk.W))

if __name__ == "__main__":
app = MainApplication()
app.mainloop()

最佳答案

我已经解决了。我没有正确初始化 tkinter.StringVar() 。我应该输入 tk.StringVar() 而不是 tk.StringVar。接下来的三行也一样。我的错。 :)

关于python - 无法使用 set() 方法设置 tk.StringVar 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com