简体   繁体   English

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

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

I am trying to create a program in Spyder that uses tkinter, everything is working as planned, however, I am having a problem getting my calculator buttons to show in by calculator window.我正在尝试在 Spyder 中创建一个使用 tkinter 的程序,一切都按计划进行,但是,我无法让我的计算器按钮显示在计算器 window 中。 The buttons do work as I have tried them in the root/home screen but when I try to add them to my calculator window i get the error "name 'calcWindow' is not defined" pops up when I execute the program, if anybody would be able to nudge me in the right direction to be able to fix this I would be extremely grateful.这些按钮确实有效,因为我在根/主屏幕中尝试过它们,但是当我尝试将它们添加到我的计算器 window 时,如果有人愿意,我会在执行程序时弹出错误“名称'calcWindow'未定义”能够将我推向正确的方向以解决此问题,我将不胜感激。

My code will be shown below.我的代码将在下面显示。

    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()

The error pops up on the line were I add "button_1" and tells me "calcWindow" is not defined but "calcWindow" is defined as a new window with "Toplevel"如果我添加“button_1”并告诉我“calcWindow”未定义但“calcWindow”被定义为带有“Toplevel”的新 window,则会弹出错误

You need to get the button placement and calls inside the function calcClick()您需要在 function 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM