簡體   English   中英

是否可以在關閉其代碼寫入單獨文件 OnClicking 按鈕的 tkinter 窗口后重新打開它?

[英]Is it possible to reopen tkinter window after closing it whose code written in separate file OnClicking button from file?

文件名 p1.py

import tkinter as tk

def action():
    import p2
root=tk.Tk()
root.title('part1')
root.geometry('200x200+50+50')
btn=tk.Button(root,text='click me',command=action)
btn.pack()
root.mainloop()

文件名 p2.py

關閉此窗口后,我想通過單擊“ click me按鈕重新打開它,但關閉此窗口后它不會打開。

import tkinter as tk
root=tk.Toplevel()
root.title('part2')
root.geometry('200x200+50+50')
lbl=tk.Label(root,text='Hello everybody \n I have problem',font=("times new roman",20,'bold'))
lbl.pack()

root.mainloop()

這里有一個解決方案:

模塊一:

import tkinter as tk


def action():
    import action_module
    action_module.page_two()


root = tk.Tk()
root.title('part1')
root.geometry('200x200+50+50')

btn = tk.Button(root, text='click me', command=action)
btn.pack()

root.mainloop()

動作模塊:

def page_two():
    import tkinter as tk

    root = tk.Toplevel()
    root.title('part2')
    root.geometry('200x200+50+50')

    lbl = tk.Label(root, text='Hello everybody \n I think the problem is fixed',
                   font=("times new roman", 20, 'bold'))
    lbl.pack()

    root.mainloop()

只需將代碼放在函數內的第二個模塊中。 然后在第一個文件的action函數中調用它。

暫無
暫無

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

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