Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python/Spyder中向新的tkinter窗口添加按钮?_Python_Tkinter_Spyder - Fatal编程技术网

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

如何在Python/Spyder中向新的tkinter窗口添加按钮?,python,tkinter,spyder,Python,Tkinter,Spyder,我试图在Spyder中创建一个使用tkinter的程序,一切都按计划进行,但是,我在让计算器按钮显示在计算器窗口中时遇到问题。按钮的工作原理与我在根/主屏幕上尝试的相同,但当我尝试将它们添加到计算器窗口时,我会在执行程序时弹出错误“name‘calcWindow’not defined”,如果有人能够推动我朝着正确的方向解决此问题,我将不胜感激 我的代码将显示在下面 from tkinter import * import tkinter as tk root = Tk()#Crea

我试图在Spyder中创建一个使用tkinter的程序,一切都按计划进行,但是,我在让计算器按钮显示在计算器窗口中时遇到问题。按钮的工作原理与我在根/主屏幕上尝试的相同,但当我尝试将它们添加到计算器窗口时,我会在执行程序时弹出错误“name‘calcWindow’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”被定义为一个具有“Toplevel”的新窗口。

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


您应该将这些按钮移动到
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)