Tkinter标签未显示在弹出窗口中

时间:2019-01-10 05:47:12

标签: python tkinter

我最近开始使用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()

由于隔离的代码段已完成应做的工作,因此似乎还缺少其他内容。

先谢谢了。 安德烈亚斯(Andreas)

1 个答案:

答案 0 :(得分:1)

主要问题是您创建了多个Tk()实例(单击button3时创建了一个新实例)。因此,将det_window更改为Toplevel的实例,并删除det_window.mainloop()函数内的调用enter_deadline()。另外,请删除Tk().withdraw()函数中的语句create_POW()

下面是经过修改的代码,其中进行了上述更改:

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()
相关问题