簡體   English   中英

Tkinter標簽未顯示在彈出窗口中

[英]Tkinter labels not showing in pop up window

我最近開始使用python進行編碼,而Stack Overflow似乎是一個來源,在那里我可能遇到的所有錯誤已經由其他人提出並得到了回答。 這次我遇到錯誤,找不到答案。

我編寫了一個應用程序,並在tkinter中使用了GUI。 在此應用程序的一部分中,用戶按下大型機上的按鈕以打開另一個窗口並輸入數據。 此后,用戶關閉彈出窗口,數據將由程序處理。 在此彈出窗口中,“條目”小部件旁邊的標簽不會出現。

我在Google上搜索了很多,並嘗試:-調用更新。 方法。 -檢查標簽是否出現在大型機中-重新啟用彈出窗口的大小調整-將代碼隔離在其他文件中,在這里標簽出現

因此,似乎主窗口中的某些內容阻止了標簽出現在彈出窗口中? 還是我忘了告訴程序要主動顯示標簽?

import tkinter as tk, sys
from tkinter import StringVar, Tk
from tkinter.filedialog import askopenfilename

root = tk.Tk()
root.title("title")
w = tk.Label(root, text="text")
w.pack()

# This is the code snippet that works isolated, but not in this context
def enter_deadline():
    det_window = tk.Tk()
    # Enter deadline
    shime_text =  StringVar()
    shime_text.set("〆切月日記入:")
    label_shime=tk.Label(det_window, textvariable=shime_text, height=6)
    label_shime.pack(side="left", padx = 20, pady=20)
    shime_val = StringVar(None)
    det_shime = tk.Entry(det_window,textvariable=shime_val, width=20)
    det_shime.pack(side="left", padx = 20, pady=20)

    def killme():
            det_window.destroy()
    det_button = tk.Button(det_window, text='スタート',command=killme).pack()
    det_window.mainloop()


# Make pop-up window for PO
def create_POW():
    try:
        Tk().withdraw()
        # show an "Open" dialog box and return the path
        po_filename = askopenfilename()
        po_file = open(po_filename, 'rb')
        enter_deadline()
    except Exception as e:
        print("ファイルエラー")
        print(e)
        sys.exit()

    # lots of repititive code for buttons
button3 = tk.Button(root, text='PO',command=create_POW).pack()

root.mainloop()

由於隔離的代碼段已完成應做的工作,因此似乎還缺少其他內容。

提前致謝。 安德烈亞斯

主要問題是您創建了多個Tk()實例(單擊button3時創建了一個新實例)。 因此,將det_window更改為Toplevel實例,並在enter_deadline()函數內刪除對det_window.mainloop()調用。 還要刪除create_POW()函數中的語句Tk().withdraw()

以下是具有上述更改的修改后的代碼:

import tkinter as tk, sys
from tkinter import StringVar, Tk
from tkinter.filedialog import askopenfilename

root = tk.Tk()
root.title("title")
w = tk.Label(root, text="text")
w.pack()

# This is the code snippet that works isolated, but not in this context
def enter_deadline():
    det_window = tk.Toplevel()  # changed from tk.Tk()
    # Enter deadline
    shime_text =  StringVar()
    shime_text.set("〆切月日記入:")
    label_shime=tk.Label(det_window, textvariable=shime_text, height=6)
    label_shime.pack(side="left", padx = 20, pady=20)
    shime_val = StringVar(None)
    det_shime = tk.Entry(det_window,textvariable=shime_val, width=20)
    det_shime.pack(side="left", padx = 20, pady=20)

    def killme():
            det_window.destroy()
    tk.Button(det_window, text='スタート',command=killme).pack()
    #det_window.mainloop()


# Make pop-up window for PO
def create_POW():
    try:
        #Tk().withdraw()
        # show an "Open" dialog box and return the path
        po_filename = askopenfilename()
        po_file = open(po_filename, 'rb')
        enter_deadline()
    except Exception as e:
        print("ファイルエラー")
        print(e)
        sys.exit()

    # lots of repititive code for buttons
button3 = tk.Button(root, text='PO',command=create_POW).pack()

root.mainloop()

暫無
暫無

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

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