簡體   English   中英

tkinter 中的第二個 window 沒有返回值

[英]second window in tkinter is not returning value

I am trying to build a login window and after logging in, a second window with a list of options appears, the user should select a number and submit it in the second window. 登錄表單運行良好,但問題是我無法從第二個 window 中提取輸入的值,並將其放入負責打印的 function Admin() 中。 我希望我很清楚,如果沒有,我會回答任何意見,謝謝。

from tkinter import *
#User experience

def Admin():
    global selection
   
    value = selection.get()
    
    print("hi",selection.get())
    if value == '1':
        print("1")
    elif value == '2':
        print("2")
    elif value == '3':
        print("3")
    elif value == '4':
        print("4")
    elif value == '5':
        print("5")
    elif value == '6':
        print("6")
    elif value == '7':
        print("7")
        

import time
incorrect = 0
def login():
    
    #skipping the if username and password matches
    AdminF()
         
#defining loginform function
def Loginform():
        global login_screen
        login_screen = Tk()
        #Setting title of screen
        login_screen.title("Login Form")
        #setting height and width of screen
        login_screen.geometry("300x250")
        #declaring variable
        

          
        global  message
        global username
        global password
        
        username = StringVar()
        password = StringVar()
        message=StringVar()
        #Creating layout of login form
        Label(login_screen,width="300", text="Please enter details below", bg="orange",fg="white").pack()
        #Username Label
        Label(login_screen, text="Username * ").place(x=20,y=40)
        #Username textbox
    
        Entry(login_screen, textvariable=username).place(x=90,y=42)
        #Password Label
        Label(login_screen, text="Password * ").place(x=20,y=80)
        #Password textbox
        Entry(login_screen, textvariable=password ,show="*").place(x=90,y=82)
        #Label for displaying login status[success/failed]
       
        #Login button
        Button(login_screen, text="Login", width=10, height=1, bg="orange",command=login).place(x=105,y=130)
        Label(login_screen, text="" ,textvariable=message).place(x=95,y=100)
        login_screen.mainloop()
        
        
class AdminF:
    

    def __init__(self):
        global selection
        selection = StringVar()
        
        
        
        display = Tk()
        display.title("ADMIN")
        display.geometry("300x250")
        label = Label(display,width="300", text ="Welcome Admin", bg="orange",fg="white")
        label.pack()

        label = Label(display,width="300", text ="Please Select a Number", bg="white",fg="black")
        label.pack()
      
        label = Label(display,width="300", text ="\n1.print 1\n\
2.print 2 \n\
3.print 3\n\
4.print 4 \n\
5.print 5\n\
6.print 6 \n\
7.print 7 ", bg="orange",fg="white")
      
        label.pack()
        
        # Label(self, text="Selection: ").place(x=20,y=180)
        Entry(display, textvariable=selection).place(x=20,y=180, width = "260")
        #selection = entry.get()
        
        Button(display, text="submit", width=10, height=1, bg="orange", command = Admin).place(x=105,y=210)
        print("\n",selection.get())
        display.mainloop
        
        
Loginform()



另一種表達方式不要使用兩個 Tk 實例,而是使用 Toplevel。 – Thingamabobs表示:

將創建第二個 window 的方式從

display = Tk()

display = Toplevel(login_screen)

您將從第二個 window 中獲得值。

display = Toplevel(login_screen)
display.grab_set()

此外,您還可以防止與login_screen window 的交互,並且這種方式還會導致從登錄創建多個 ADMIN windows 引起的所有對提交按鈕的反應,但只有一個提供正確的用戶輸入值。

在此處輸入圖像描述

在上圖中,單擊右提交按鈕將給出值4而不是最終預期的7

也許對 tkinter 有更深入了解的人可以在評論中指出如何在附加創建的 windows 中正確處理按鈕按下事件,但對條目內容的更改不是?

有關在 tkinter 中創建多個 windows 的更多詳細信息,請參閱https: //www.pythontutorial.net/tkinter/tkinter-toplevel/。

順便說一句:

display.mainloop

命令可以從你的代碼中刪除,以避免在猜測它的作用以及為什么它是必要的時候造成混淆。

暫無
暫無

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

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