python - 如何在新窗口中获取事件以使用 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()
解决方案
当您在函数内定义变量时,它的使用仅限于该函数。在这里,您已经在函数中定义了一个局部变量typeSpace
,因此当您在函数以外的函数中进行任何更改时,程序会返回未定义的错误。calcWindow
calcClick()
typeSpace
typeSpace
calcClick()
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)
- 按钮
button_plus
,button_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()
希望对您有所帮助...
推荐阅读
- c++ - 使用钩子向 explorer.exe 主窗口发送消息会导致崩溃?
- python - 如何修复 matplotlib 中所有子图的图例和主标题?
- javascript - Django 不提供开发中的所有 js 文件
- typescript - 函数重载编译错误(此重载签名不兼容)与模板文字类型
- javascript - 使用 MySQL 和 Node.js 从特定类别中获取每个项目的最佳实践是什么?
- next.js - 为什么我的会话在包含刷新令牌时会被清空?
- python - 根据 pyspark 数组列中的多个字符串进行过滤
- python - 从键值对替换数据帧中的子字符串的最快方法
- javascript - How to assign class to template ref? -- VueJS 3 Typescript Component API
- neural-network - 为 PyTorch 数据集创建自定义图像