所以,我的python程序中有一些不同的小部件:infobar
和texteditor
在我的App
类中;我的commandline
课程中output
和Terminal
。但是当我运行程序时,命令行和输出显示在我的App窗口中。
class Terminal(Frame):
### A window used for running Terminal commands.
def __init__(self, master):
### Initialize the Terminal app.
# Initialize frame
frame = Frame(terminalroot)
frame.pack()
# Create fonts
txtFont = tkFont.Font(family="Monaco",size=10)
# Initilize Commandline.
self.commandline = Text(wrap=None,font=txtFont,width=40,height=1)
self.commandline.pack()
# Initialize Output.
self.output = Text(wrap=WORD,font=txtFont,width=40,height=30)
self.output.pack()
class App(Frame):
### The main application window.
def __init__(self, master):
### Initialize the program.
## Initialize the frame
## Setup the frame.
## Set the title of the window.
## Pack the frame.
## Initialize random class variables.
## Create fonts.
## txtFont: Monaco, size 10. Used for most of the text areas.
## Create and pack the editor.
## Wrap text along words.
## Set size to W120 and H20.
## Insert a short message to the text editor.
## Reset any undo/redos.
## Pack the editor.
## Setup tags.
## Alert, sel.
## Create and pack the info bar.
## Set size to W120 and H1.
## Insert Info bar to the info bar.
## Setup the menu bar.
## File:
## Save, ^S, save_it
## Save as, ^[shift]S, save_it_as
## Load, ^L, load_it_and_clear
## Import, ^I, load_it
## Edit:
## Undo, ^Z, undo
## Redo, ^Y, redo
## Style:
## Default, no shortcut, set_theme_default
## White on Black, no shortcut, set_theme_wob
## Homebrew, no shortcut, set_theme_hbrew
## Setup the popup menu.
## Undo, ^Z, undo
## Redo, ^Y, redo
## Bind various commands.
## Final configuration.
self.frame = Frame(root, background="black")
root.title("BasicEdit")
self.frame.pack()
self.filename = "Hello World.txt"
txtFont = tkFont.Font(family="Monaco", size=10)
self.texteditor = Text(wrap=WORD,font=txtFont,width=120,height=20)
self.texteditor.insert(END, """Lorem epsum...""")
self.texteditor.edit_reset()
self.texteditor.pack()
self.texteditor.tag_config("alert", foreground="white", background="black")
self.texteditor.tag_config("sel", foreground="blue")
self.infobar = Text(font=txtFont,height=1,width=120)
self.infobar.insert(END, "Info bar")
self.infobar.pack()
self.menubar = Menu()
self.filemenu = Menu(self.menubar, tearoff=0)
self.filemenu.add_command(label="Save", command=self.save_it, accelerator="Ctrl+S")
self.filemenu.add_command(label="Save as", command=self.save_it_as, accelerator="Ctrl+Shift+S")
self.filemenu.add_command(label="Load", command=self.load_it_and_clear, accelerator="Ctrl+L")
self.filemenu.add_command(label="Import", command=self.load_it, accelerator="Ctrl+I")
self.editmenu = Menu(self.menubar, tearoff=0)
self.editmenu.add_command(label="Undo", command=self.undo, accelerator="Ctrl+Z")
self.editmenu.add_command(label="Redo", command=self.redo, accelerator="Ctrl+Y")
self.stylemenu = Menu(self.menubar, tearoff=0)
self.stylemenu.add_command(label="Default", command=self.set_theme_default)
self.stylemenu.add_command(label="White on Black", command=self.set_theme_wob)
self.stylemenu.add_command(label="Homebrew", command=self.set_theme_hbrew)
self.menubar.add_cascade(label="File", menu=self.filemenu)
self.menubar.add_cascade(label="Edit", menu=self.editmenu)
self.menubar.add_cascade(label="Style", menu=self.stylemenu)
self.popup = Menu(tearoff=1)
self.popup.add_command(label="Undo", command=self.undo, accelerator="Ctrl+Z")
self.popup.add_command(label="Redo", command=self.redo, accelerator="Ctrl+Y")
root.bind("<Button-2>", self.rightmenu)
root.bind("<Control-q>", self.quit)
root.bind("<Control-Q>", self.quit)
root.bind("<Control-s>", self.save_it)
root.bind("<Control-S>", self.save_it)
root.bind("<Control-l>", self.load_it_and_clear)
root.bind("<Control-L>", self.load_it_and_clear)
root.bind("<Control-i>", self.load_it)
root.bind("<Control-I>", self.load_it)
root.bind("<Control-z>", self.undo)
root.bind("<Control-Z>", self.undo)
root.bind("<Control-y>", self.redo)
root.bind("<Control-Y>", self.redo)
root.bind("<Key>", self.keyPress)
self.set_theme_default()
root.config(menu=self.menubar)
请注意,除了将我的小部件放在右侧窗口中之外,所有代码都有效。 Raw Paste
答案 0 :(得分:2)
如果您没有告诉窗口小部件它的父窗口是什么,则默认为根窗口。在许多小部件中,您没有设置父窗口。您应始终定义父窗口。
在继承自Frame
或Toplevel
的类中定义小部件时尤其如此。当你这样做时,假设这个类是自包含的 - 所有东西都是那个类的孩子。当某些小部件成为根窗口的子代时,您的代码变得难以掌握,因为您无法对哪些小部件属于哪些父级进行任何假设。
注意您的终端类是Frame
的子类,但是您将命令行和输出窗口放在根窗口内(由于不将其放在其他位置) :
class Terminal(Frame):
def __init__(self, master):
...
self.commandline = Text(wrap=None,font=txtFont,width=40,height=1)
...
self.output = Text(wrap=WORD,font=txtFont,width=40,height=30)
同样地:
class App(Frame):
def __init__(self, master):
...
self.texteditor = Text(wrap=WORD,font=txtFont,width=120,height=20)
上述每个文本窗口小部件都应使用self
作为Text
窗口小部件的父窗口,以便子窗口小部件显示在Frame
窗口小部件中。例如:
self.texteditor = text(self, wrap=WORD, ...)