如何在 Python/Spyder 中向新的 tkinter window 添加按钮?

How to add buttons to a new tkinter window in Python/Spyder?

我正在尝试在 Spyder 中创建一个使用 tkinter 的程序,一切都按计划进行,但是,我在让计算器按钮显示在计算器 window 中时遇到问题。这些按钮确实有效,因为我在 root/home 屏幕中尝试过它们,但是当我尝试将它们添加到我的计算器 window 时,我在执行程序时收到错误 "name 'calcWindow' is not defined" 弹出,如果任何人都可以将我推向正确的方向以解决此问题,我将不胜感激。

我的代码如下所示。

    from tkinter import * 
import tkinter as tk

root = Tk()#Creates root widget 
root.title("Welcome Screen")#Names the welcome screen 
welcomeLabel = Label (root, text="Hello user, welcome to the text input and calculator program")#Creates label widget 
welcomeLabel.grid(row=0, column=0)#Casts label to screen in specified position

textInputButton = Button(root, text="Text Input Mode", command=tiClick, fg='red')#adds button
textInputButton.grid(row=1, column=10)#Positions button 

calc = Button(root, text="Calculator Mode", command=calcClick, fg='red')#adds button
calc.grid(row=2, column=10)#Positions button   

def tiClick():
     tInputWindow = Toplevel()#opens a new window for the text input mode   
     tInputWindow.title("Text Input Mode")#Names the new window 

def calcClick():
    calcWindow = Toplevel()#opens a new window for Calculator mode   
    calcWindow.title("Calculator Mode")#Names the new window    

def button_add():
    return         

#Defines calculator buttons and places them in grid setting on screen
button_1 = Button(calcWindow, text="1", padx=40, pady=20 , command=button_add).grid(row=3, column=0)
button_2 = Button(calcWindow, text="2", padx=40, pady=20 , command=button_add).grid(row=3, column=1)
button_3 = Button(calcWindow, text="3", padx=40, pady=20 , command=button_add).grid(row=3, column=2)
button_4 = Button(calcWindow, text="4", padx=40, pady=20 , command=button_add).grid(row=2, column=0)
button_5 = Button(calcWindow, text="5", padx=40, pady=20 , command=button_add).grid(row=2, column=1)
button_6 = Button(calcWindow, text="6", padx=40, pady=20 , command=button_add).grid(row=2, column=2)
button_7 = Button(calcWindow, text="7", padx=40, pady=20 , command=button_add).grid(row=1, column=0)
button_8 = Button(calcWindow, text="8", padx=40, pady=20 , command=button_add).grid(row=1, column=1)
button_9 = Button(calcWindow, text="9", padx=40, pady=20 , command=button_add).grid(row=1, column=2)
button_0 = Button(calcWindow, text="0", padx=40, pady=20 , command=button_add).grid(row=4, column=0)  

root.mainloop()

当我添加 "button_1" 时,错误会弹出并告诉我 "calcWindow" 未定义,但 "calcWindow" 被定义为新的 window 和 "Toplevel"

您需要在函数 calcClick() 中获取按钮放置和调用

def calcClick():
    calcWindow = Toplevel()#opens a new window for Calculator mode   
    calcWindow.title("Calculator Mode")#Names the new window   
    #Defines calculator buttons and places them in grid setting on screen
    button_1 = Button(calcWindow, text="1", padx=40, pady=20 , command=button_add).grid(row=3, column=0)
    button_2 = Button(calcWindow, text="2", padx=40, pady=20 , command=button_add).grid(row=3, column=1)
    button_3 = Button(calcWindow, text="3", padx=40, pady=20 , command=button_add).grid(row=3, column=2)
    button_4 = Button(calcWindow, text="4", padx=40, pady=20 , command=button_add).grid(row=2, column=0)
    button_5 = Button(calcWindow, text="5", padx=40, pady=20 , command=button_add).grid(row=2, column=1)
    button_6 = Button(calcWindow, text="6", padx=40, pady=20 , command=button_add).grid(row=2, column=2)
    button_7 = Button(calcWindow, text="7", padx=40, pady=20 , command=button_add).grid(row=1, column=0)
    button_8 = Button(calcWindow, text="8", padx=40, pady=20 , command=button_add).grid(row=1, column=1)
    button_9 = Button(calcWindow, text="9", padx=40, pady=20 , command=button_add).grid(row=1, column=2)
    button_0 = Button(calcWindow, text="0", padx=40, pady=20 , command=button_add).grid(row=4, column=0)