在其他函数中找不到 .set in 函数,因此它正在创建一个错误 tkinter python



我正在尝试创建一个 GUI,在导航菜单中,您可以单击级联选项以打开另一个窗口,您可以在其中单击滚动以生成一组数字。它出现了错误。我认为这是因为该函数是从另一个函数调用的,我只是不知道如何让该函数调用它/是否有任何其他方法可以解决此问题。我已经尝试了全局函数并查找了它,但到目前为止除了使用类之外没有找到任何其他内容,我不知道该怎么做。

line 147, in totalRolls
txtresultsOut.set(totalRollResults)
NameError: name 'txtresultsOut' is not defined

这是与之相关的代码。我已经调用了该函数以跳过为主 gui 窗口输入所有其他代码。

def rollSix():
s = 0
numbers = [0,0,0,0]
for i in range(1,5):
numbers[s] = randrange(1,7)
s += 1
numbers.remove(min(numbers))
Result = sum(numbers)
totalRollResults.append(Result)
def totalRolls():
rollOne()
rollTwo()
rollThree()
rollFour()
rollFive()
rollSix()
txtresultsOut.set(totalRollResults)
def rollw():
rollWindow = tix.Tk()
rollWindow.title("Dice Rolls")
diceLabel = Label(rollWindow, text = "Click Roll for your Stats")
diceLabel.grid(row = 0, column = 0)
rollBtn = Button(rollWindow, text = "Roll Stats", command = totalRolls)
rollBtn.grid(row = 1, column = 0)
txtresultsOut = StringVar()
resultsOut = Entry(rollWindow, state = "readonly", textvariable = txtresultsOut)
resultsOut.grid(row = 2, column = 0)
rollw()

首先我不建议使用StringVar()。您可以使用.get()方法Entry来获取相同的值。尝试这种方式,并对要在其他函数中获取其值的条目进行global声明。

编辑------------

#you can use the following code to make your entry active to be edited.
entry.configure(state='normal')
# insert new values after deleting old ones (down below)
entry.delete(0,END)
entry.insert(0, text_should_be_here)
# and finally make its state readonly to not let the user mess with the entry
entry.configure(state='readonly')

最新更新