首页 > 解决方案 > 如何在新窗口中获取事件以使用 tkinter Python 正常运行?

问题描述

从今天早上开始,这个程序就出现了问题,它似乎只是代码的放置而不是代码本身,我试图在一个与根窗口分开的新 tkinter 窗口中运行一个计算器,并且一直在处理代码尝试让它运行无济于事,不断弹出的错误似乎是基于未定义的“typeSpace”或“calcWindow”,但它们显然是,有人知道如何正确放置此代码以便它识别这些变量在运行时正确吗?

from tkinter import *
import tkinter as tk
#def onButton_Plus():
#        global one_Num#Declares global variable
#        number_One = typeSpace.get()#Gets number typed on calculator by user
#        one_Num = int(number_One)
#        typeSpace.delete(0, END)
def calcClick():

    calcWindow = Toplevel()##opens a new window for Calculator mode
    ##Creates a empty field in the calculator page for numbers to be displayed
    typeSpace = Entry(calcWindow, width=35, borderwidth=5)
    typeSpace.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
    ##Names the new window
    calcWindow.title("Calculator Mode")

    ##Defines calculator buttons and places them in grid setting on screen
    button_1 = Button(calcWindow, text="1", padx=40, pady=20, command=lambda: onButton_Click(1)).grid(row=3, column=0)
    button_2 = Button(calcWindow, text="2", padx=40, pady=20, command=lambda: onButton_Click(2)).grid(row=3, column=1)
    button_3 = Button(calcWindow, text="3", padx=40, pady=20, command=lambda: onButton_Click(3)).grid(row=3, column=2)
    button_4 = Button(calcWindow, text="4", padx=40, pady=20, command=lambda: onButton_Click(4)).grid(row=2, column=0)
    button_5 = Button(calcWindow, text="5", padx=40, pady=20, command=lambda: onButton_Click(5)).grid(row=2, column=1)
    button_6 = Button(calcWindow, text="6", padx=40, pady=20, command=lambda: onButton_Click(6)).grid(row=2, column=2)
    button_7 = Button(calcWindow, text="7", padx=40, pady=20, command=lambda: onButton_Click(7)).grid(row=1, column=0)
    button_8 = Button(calcWindow, text="8", padx=40, pady=20, command=lambda: onButton_Click(8)).grid(row=1, column=1)
    button_9 = Button(calcWindow, text="9", padx=40, pady=20, command=lambda: onButton_Click(9)).grid(row=1, column=2)
    button_0 = Button(calcWindow, text="0", padx=40, pady=20, command=lambda: onButton_Click(0)).grid(row=4, column=0)
    button_plus = Button(calcWindow, text="+", padx=39, pady=20, command=lambda: onButton_Click()).grid(row=5, column=0)
    button_equal = Button(calcWindow, text="=", padx=91, pady=20, command=lambda: onButton_Click()).grid(row=5, column=1,columnspan=2)
    button_clear = Button(calcWindow, text="Clear", padx=79, pady=20, command= onButton_Clear()).grid(row=4, column=1, columnspan=2)
def tiClick():
     tInputWindow = Toplevel()##opens a new window for the text input mode
     tInputWindow.title("Text Input Mode")##Names the new window


def onButton_Click(number):

    onNo = typeSpace.get()##gets the number pressed by the user
    typeSpace.delete(0, END)##deletes previous numbers typed
    typeSpace.insert(0, str(onNo) + str(number))##Inserts the number clicked into the field
def onButton_Clear():##Defines what the clear button does when clicked

    typeSpace.delete(0, END)##deletes all previous numbers typed

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

root.mainloop()

标签: pythontkinter

解决方案


当您在函数内定义变量时,它的使用仅限于该函数。在这里,您已经在函数中定义了一个局部变量typeSpace,因此当您在函数以外的函数中进行任何更改时,程序会返回未定义的错误。calcWindowcalcClick()typeSpacetypeSpacecalcClick()typeSpace

为了在函数之外使用它定义的局部变量,你只需要给它一个全局变量的状态:

global calcWindow
calcWindow = Toplevel()

global typeSpace
typeSpace = Entry(calcWindow, width=35, borderwidth=5)
typeSpace.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
  1. 按钮button_plusbutton_equal并且button_clear不起作用。该函数onButton_Click(number)将不接受空条目。此外,如果您插入数字以外的值(如 +、= 或清除)
.
.
    button_plus = Button(calcWindow, text="+", padx=39, pady=20, command=lambda: onButton_Click(+)).grid(row=5, column=0)
    button_equal = Button(calcWindow, text="=", padx=91, pady=20, command=lambda: onButton_Click("=")).grid(row=5, column=1,columnspan=2)
    button_clear = Button(calcWindow, text="Clear", padx=79, pady=20, command= onButton_Clear("Clear")).grid(row=4, column=1, columnspan=2)
.
.

程序将返回错误

如果您再次遇到任何麻烦,请参阅此

from tkinter import *
import tkinter as tk




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





#CALCULATOR MODE
def onButton_Plus():
    global one_Num #Declares global variable
    number_One = typeSpace.get()#Gets number typed on calculator by user
    one_Num = int(number_One)
    typeSpace.delete(0, END)
def calcClick():
    calcWindow = Toplevel()##opens a new window for Calculator mode
    ##Creates a empty field in the calculator page for numbers to be displayed
    global typeSpace
    typeSpace = Entry(calcWindow, width=35, borderwidth=5)
    typeSpace.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
    ##Names the new window
    calcWindow.title("Calculator Mode")

    ##Defines calculator buttons and places them in grid setting on screen
    button_1 = Button(calcWindow, text="1", padx=40, pady=20, command=lambda: onButton_Click(1)).grid(row=3, column=0)
    button_2 = Button(calcWindow, text="2", padx=40, pady=20, command=lambda: onButton_Click(2)).grid(row=3, column=1)
    button_3 = Button(calcWindow, text="3", padx=40, pady=20, command=lambda: onButton_Click(3)).grid(row=3, column=2)
    button_4 = Button(calcWindow, text="4", padx=40, pady=20, command=lambda: onButton_Click(4)).grid(row=2, column=0)
    button_5 = Button(calcWindow, text="5", padx=40, pady=20, command=lambda: onButton_Click(5)).grid(row=2, column=1)
    button_6 = Button(calcWindow, text="6", padx=40, pady=20, command=lambda: onButton_Click(6)).grid(row=2, column=2)
    button_7 = Button(calcWindow, text="7", padx=40, pady=20, command=lambda: onButton_Click(7)).grid(row=1, column=0)
    button_8 = Button(calcWindow, text="8", padx=40, pady=20, command=lambda: onButton_Click(8)).grid(row=1, column=1)
    button_9 = Button(calcWindow, text="9", padx=40, pady=20, command=lambda: onButton_Click(9)).grid(row=1, column=2)
    button_0 = Button(calcWindow, text="0", padx=40, pady=20, command=lambda: onButton_Click(0)).grid(row=4, column=0)
    button_plus = Button(calcWindow, text="+", padx=39, pady=20, command=onButton_Plus).grid(row=5, column=0)
    button_equal = Button(calcWindow, text="=", padx=91, pady=20, command=onButton_Equals).grid(row=5, column=1,columnspan=2)
    button_clear = Button(calcWindow, text="Clear", padx=79, pady=20, command= onButton_Clear).grid(row=4, column=1, columnspan=2)

def onButton_Click(number):
    onNo = typeSpace.get()##gets the number pressed by the user
    typeSpace.delete(0, END)##deletes previous numbers typed
    typeSpace.insert(0, str(onNo) + str(number))##Inserts the number clicked into the field

def onButton_Clear():##Defines what the clear button does when clicked
    typeSpace.delete(0, END)##deletes all previous numbers typed

def onButton_Equals():
    sec_Num = typeSpace.get()
    typeSpace.delete(0, END)
    sec_Num = int(sec_Num)
    ANSWER = one_Num+sec_Num
    typeSpace.insert(0, ANSWER)

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

root.mainloop()

希望对您有所帮助...


推荐阅读