簡體   English   中英

如何使用 tkinter ZA7F5F35426B927411FC9231B7 正確獲取新 windows 到 function 中的事件

[英]How to get events in new windows to function correctly with tkinter Python?

從今天早上開始,這個程序就出現了問題,它似乎只是代碼的放置而不是代碼本身,我試圖在一個新的 tkinter window 中運行一個計算器,與根 window 分開並且一直在玩弄代碼嘗試將其發送到 function 無濟於事,不斷彈出的錯誤似乎是基於未定義的“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()

當您在 function 中定義變量時,它的使用僅限於 function。 Here you have defined typeSpace and calcWindow in the calcClick() function,so typeSpace is a local variable and thus when you make any changes to typeSpace in a function other than calcClick() function, the program returns the error of typeSpace not being defined.

為了在函數之外使用它定義的局部變量,你只需要給它一個全局變量的狀態:

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_equalbutton_clear將不起作用。 function 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()

希望對您有所幫助...

這里。 我重新編寫了您的代碼並使其不那么混亂。

代碼:

from tkinter import *
#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()


    typeSpace = Entry(calcWindow, width=35, borderwidth=5)
    typeSpace.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
    calcWindow.title("Calculator Mode")

    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 equal_clicked():
        print(eval(typeSpace.get()))
    def onButton_Clear():##Defines what the clear button does when clicked
        typeSpace.delete(0, END)


    ##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: equal_clicked()).grid(row=5, column=1,columnspan=2)
    button_clear = Button(calcWindow, text="Clear", padx=79, pady=20, command= lambda: 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



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

當您單擊等號按鈕時,結果將打印到 shell。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM