无法定义' textvariable'在tkinter的自我参考实现中

时间:2014-05-14 15:00:38

标签: python tkinter

我一再尝试并且失败了解我应该在何处以及如何定义textvariable" die"在以下代码中。注释行是我尝试分配值的地方,但是各种值或名称错误不断发生,这让我相信我不知道如何正确初始化其值。

from random import *
from tkinter import *
from tkinter import ttk
import tkinter as tk

#die = StringVar() # 1

def roll (int):
    try:
        die.set(randrange(1,int))
    except ValueError:
        pass

class Application(ttk.Frame):

    #die = StringVar() # 2

    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)   
        self.grid()
        #self.die = StringVar() # 3
        self.createWidgets()

    def createWidgets(self):
        #self.die = StringVar() # 4
        self.d_twoButton = ttk.Button(self, text='d2', command=roll(2)).grid()
        self.d_threeButton = ttk.Button(self, text='d3', command=roll(3)).grid()
        self.d_fourButton = ttk.Button(self, text='d4', command=roll(4)).grid()
        self.d_sixButton = ttk.Button(self, text='d6', command=roll(6)).grid()
        self.d_eightButton = ttk.Button(self, text='d8', command=roll(8)).grid()
        self.d_tenButton = ttk.Button(self, text='d10', command=roll(10)).grid()
        self.d_twelveButton = ttk.Button(self, text='d12', command=roll(12)).grid()
        self.d_twentyButton = ttk.Button(self, text='d20', command=roll(20)).grid()
        self.d_onehundredButton = ttk.Button(self, text='d100', command=roll(100)).grid()
        self.resultLabel = ttk.Label(self, textvariable=die)
        self.resultLabel.grid()        

app = Application()                      
app.master.title('Die Roller')
#app.die = StringVar() # 5
app.mainloop() 

(我曾尝试为textvariable和stringval添加标签以便于识别,但我现在还不能。如果有人能够在这篇文章中添加这样的标签,请随意这样做。)

1 个答案:

答案 0 :(得分:1)

Spot#3可能是最好的地方。但是,您需要进行一些额外的修改。

  • 您必须明确引用roll中的app对象才能访问其die属性。或者,roll应该是Application类的方法;见第二个代码块。
  • command参数必须包含在lambda中,如果您要为要调用它的函数提供参数。
  • 可选:删除所有self.d_someNumberButton =分配位,因为它们都是None,并且您无论如何都不会在任何地方使用它们。 (提示:my_thing = Button()my_thing变为按钮。my_thing = Button().grid()my_thing变为None。)

对原始代码进行尽可能少的修改后实施:

from random import *
from tkinter import *
from tkinter import ttk
import tkinter as tk

#die = StringVar() # 1

def roll (int):
    try:
        app.die.set(randrange(1,int))
    except ValueError:
        pass

class Application(ttk.Frame):

    #die = StringVar() # 2

    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)   
        self.grid()
        self.die = StringVar() # 3
        self.createWidgets()

    def createWidgets(self):
        #self.die = StringVar() # 4
        self.d_twoButton = ttk.Button(self, text='d2', command=lambda: roll(2)).grid()
        self.d_threeButton = ttk.Button(self, text='d3', command=lambda: roll(3)).grid()
        self.d_fourButton = ttk.Button(self, text='d4', command=lambda: roll(4)).grid()
        self.d_sixButton = ttk.Button(self, text='d6', command=lambda: roll(6)).grid()
        self.d_eightButton = ttk.Button(self, text='d8', command=lambda: roll(8)).grid()
        self.d_tenButton = ttk.Button(self, text='d10', command=lambda: roll(10)).grid()
        self.d_twelveButton = ttk.Button(self, text='d12', command=lambda: roll(12)).grid()
        self.d_twentyButton = ttk.Button(self, text='d20', command=lambda: roll(20)).grid()
        self.d_onehundredButton = ttk.Button(self, text='d100', command=lambda: roll(100)).grid()
        self.resultLabel = ttk.Label(self, textvariable=self.die)
        self.resultLabel.grid()        

app = Application()                      
app.master.title('Die Roller')
#app.die = StringVar() # 5
app.mainloop() 

结果:

enter image description here

在这里,我只点击了“d100”按钮,它成功显示了预期范围内的数字。


使用roll作为类方法的替代实现:

from random import *
from tkinter import *
from tkinter import ttk
import tkinter as tk

class Application(ttk.Frame):
    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)   
        self.grid()
        self.die = StringVar()
        self.createWidgets()

    def createWidgets(self):
        ttk.Button(self, text='d2', command=lambda: self.roll(2)).grid()
        ttk.Button(self, text='d3', command=lambda: self.roll(3)).grid()
        ttk.Button(self, text='d4', command=lambda: self.roll(4)).grid()
        ttk.Button(self, text='d6', command=lambda: self.roll(6)).grid()
        ttk.Button(self, text='d8', command=lambda: self.roll(8)).grid()
        ttk.Button(self, text='d10', command=lambda: self.roll(10)).grid()
        ttk.Button(self, text='d12', command=lambda: self.roll(12)).grid()
        ttk.Button(self, text='d20', command=lambda: self.roll(20)).grid()
        ttk.Button(self, text='d100', command=lambda: self.roll(100)).grid()
        self.resultLabel = ttk.Label(self, textvariable=self.die)
        self.resultLabel.grid()        

    def roll(self, max_value):
        app.die.set(randrange(1,max_value))

app = Application()                      
app.master.title('Die Roller')
app.mainloop() 

使用for循环创建按钮的替代替代实现:

from random import *
from tkinter import *
from tkinter import ttk
import tkinter as tk

class Application(ttk.Frame):
    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)   
        self.grid()
        self.die = StringVar()
        self.createWidgets()

    def createWidgets(self):
        for num_sides in [2, 3, 4, 6, 8, 10, 12, 20, 100]:
            ttk.Button(self, text="d{}".format(num_sides), command=lambda value=num_sides: self.roll(value)).grid()
        self.resultLabel = ttk.Label(self, textvariable=self.die)
        self.resultLabel.grid()        

    def roll(self, max_value):
        app.die.set(randrange(1,max_value))

app = Application()                      
app.master.title('Die Roller')
app.mainloop()