英文:
Problem with packing widget in another window
问题
I have a problem, I need to on button clicking pack terminal from tkterminal library in new window and destroy it(like toggle), but terminal is packing in already exists "root" window, so there is a code:
def open_terminal():
global debounce
if debounce == True:
global cmd
cmd = tk.Toplevel(root)
cmd.title("Terminal")
global terminal
terminal = Terminal(pady=5, padx=5)
terminal.basename = "dolphinwriter$"
terminal.shell = True
terminal.pack(cmd, side="top", fill="both", expand=True)
debounce = False
print(debounce)
else:
cmd.destroy()
debounce = True
I'm trying this:
- Checking if already terminal packed in root window and if yes, unpack it and pack it again
- Upacking terminal and pack it again without checking.
I tried to do everything described above with the pack_forget() method
英文:
I have a problem, I need to on button clicking pack terminal from tkterminal library in new window and destroy it(like toggle), but terminal is packing in already exists "root" window, so there is a code:
def open_terminal():
global debounce
if debounce == True:
global cmd
cmd = tk.Toplevel(root)
cmd.title("Terminal")
global terminal
terminal = Terminal(pady=5, padx=5)
terminal.basename = "dolphinwriter$"
terminal.shell = True
terminal.pack(cmd, side="top", fill="both", expand=True)
debounce = False
print(debounce)
else:
cmd.destroy()
debounce = True
I'm trying this:
- Checking if already terminal packed in root window and if yes, unpack it and pack it again
- Upacking terminal and pack it again without checking.
I tried to do everything described above with the pack_forget() method
答案1
得分: 0
The problem is that you defined the terminals master window in the line where you pack it: terminal.pack(cmd, side="top", fill="both", expand=True)
.
In Tkinter, assigning the master window is done when you create the object.
Corrected code:
def open_terminal():
global debounce
if debounce == True:
global cmd
cmd = tk.Toplevel(root)
cmd.title("Terminal")
global terminal
terminal = Terminal(master=cmd, pady=5, padx=5)
terminal.basename = "dolphinwriter$"
terminal.shell = True
terminal.pack(side="top", fill="both", expand=True)
debounce = False
print(debounce)
else:
cmd.destroy()
debounce = True
Should fix it.
英文:
The problem is that you defined the terminals master window in the line where you pack it: terminal.pack(cmd, side="top", fill="both", expand=True)
.
In Tkinter, assigning the master window is done when you create the object.
Corrected code:
def open_terminal():
global debounce
if debounce == True:
global cmd
cmd = tk.Toplevel(root)
cmd.title("Terminal")
global terminal
terminal = Terminal(master=cmd, pady=5, padx=5)
terminal.basename = "dolphinwriter$"
terminal.shell = True
terminal.pack(side="top", fill="both", expand=True)
debounce = False
print(debounce)
else:
cmd.destroy()
debounce = True
Should fix it.
答案2
得分: 0
要将terminal
放在Toplevel
中,您需要显式指定master
,默认情况下小部件具有root
父级。
import tkinter as tk
from tkterminal import Terminal
from functools import partial
def open_terminal():
root.withdraw()
cmd = tk.Toplevel(root)
cmd.title("Terminal")
cmd.protocol("WM_DELETE_WINDOW", partial(on_closing, cmd))
terminal = Terminal(cmd, pady=5, padx=5)
terminal.basename = "dolphinwriter$"
terminal.shell = True
terminal.pack(side="top", fill="both", expand=True)
def on_closing(cmd):
cmd.destroy()
root.deiconify()
root = tk.Tk()
btn = tk.Button(root, text="new_window", fg="Black", command=open_terminal)
btn.pack()
root.mainloop()
英文:
To place terminal
in Toplevel
, you need to explicitly specify master
, by default widgets have root
parent.
import tkinter as tk
from tkterminal import Terminal
from functools import partial
def open_terminal():
root.withdraw()
cmd = tk.Toplevel(root)
cmd.title("Terminal")
cmd.protocol("WM_DELETE_WINDOW", partial(on_closing, cmd))
terminal = Terminal(cmd, pady=5, padx=5)
terminal.basename = "dolphinwriter$"
terminal.shell = True
terminal.pack(side="top", fill="both", expand=True)
def on_closing(cmd):
cmd.destroy()
root.deiconify()
root = tk.Tk()
btn = tk.Button(root, text="new_window", fg="Black", command=open_terminal)
btn.pack()
root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论