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

时间:2019-01-24 11:18:40

标签: python python-3.x tkinter

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

  

TypeError:set()缺少1个必需的位置参数:“ 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()

1 个答案:

答案 0 :(得分:2)

我知道了。我没有正确初始化tkinter.StrinVar()。 我应该键入tk.StringVar()而不是tk.StringVar。接下来的三行也一样。我的错。 :)