寫了一個tk 程式如下,但遇到兩個問題一直想解不掉,想請前輩們指導
問題1:使用admin_root.destroy(),為什麼關不掉視窗?只能關掉Frame呢?
問題2:為何ID.get()取得的是空值呢?
import tkinter as tk
from tkinter import ttk
from tkinter import Frame
from tkinter import messagebox
import os
#ID內容判斷
def ID_judg():
aa = ID.get() ############問題2:取得的是空值?
messagebox.showinfo(aa)
#建立介面
def ID_ADD():
global ID
id_Root=tk.Tk()
id_Root.title("建立")
id_Root.geometry("300x150+380+380")
id_Root.resizable(width=False, height=False)
userlable = tk.Label(id_Root,
text = "請輸入文字",
font = ('微軟正黑體', 12) )
userlable.grid(row=0, column=0,columnspan=2,pady= 10)
ID = tk.StringVar()
userID = tk.Entry(id_Root, textvariable=ID,width = 15)
userID.grid(row=1, column=0,columnspan=2,pady= 10)
BUT1 = tk.Button(id_Root, text = "送出", height = 1, bg = "AliceBlue",width = 10,command=ID_judg)
BUT1.grid(row=2, column=0,padx= 35,pady= 10)
BUT2 = tk.Button(id_Root, text = "結束", height = 1, bg = "AliceBlue",width = 10,command=id_Root.destroy)
BUT2.grid(row=2, column=1,padx= 35,pady= 10)
id_Root.mainloop()
# 判斷密碼
def netest():
number = entry.get()
if number != "123456":
messagebox.showerror("錯誤", "密碼錯誤")
else:
admin_root.destroy() ############問題1: 為什麼關不掉視窗?只能關掉Frame?
ID_ADD()
admin_root=tk.Tk()
admin_root.title("login check")
admin_root.geometry("600x270+380+380")
admin_root.resizable(width=False, height=False)
admin_root=ttk.Notebook()
admin_root.place(relx=0.02, rely=0.02, relwidth=0.95, relheight=0.876)
tab1=Frame(admin_root)
tab1.place(x=0,y=30)
admin_root.add(tab1,text='User')
label = tk.Label(tab1,
text = "請輸入密碼",
font = ('微軟正黑體', 13) )
label.pack(pady=20)
entry = tk.Entry(tab1, width = 15,show='*')
entry.pack(pady=7)
tk.Button(tab1, text = "送出", height = 2, bg = "AliceBlue",width = 10,command=netest).pack(pady=7)
tab2=Frame(admin_root)
tab2.place(x=0,y=30)
admin_root.add(tab2,text='Admin')
label = tk.Label(tab2,
text = "frame2",
font = ('微軟正黑體', 13) )
label.pack(pady=20)
admin_root.mainloop()
問題1
admin_root.destroy()
改為
global admin_root
admin_root.destroy()
問題2
aa = ID.get()
改為
global ID
aa = ID.get()