创建窗口上的小部件显示在父窗口上
我有一个弹出选项窗口的按钮,当我尝试在新窗口上填充小部件时,它们仅出现在父窗口上。
这是相关代码:
from Tkinter import *
class MainWindow:
def __init__(self, master):
"""builds main window."""
windowAttr = {"width":450, "height":150}
window = Frame(master, windowAttr).grid()
btnAttr = {"text":"Options", "width":12, "height":1}
self.btnOptions = Button(window, btnAttr, command=btnOptionsClick).place(x=360, y=5)
class Options:
def __init__(self, optMaster):
"""Builds and displays the options window"""
optAttr = {"width":300, "height":200}
optWin = Frame(optMaster, optAttr).grid()
self.chkMon = Checkbutton(optWin, text="Mon").place(x=50, y=50)
def btnOptionsClick():
opt = Tk(className='Options')
optionsApp = Options(opt)
opt.mainloop()
root = Tk(className='Main Window')
app = MainWindow(root)
root.mainloop()
chkMon 出现在主窗口上,选项始终为空,我希望 chkMon 出现在选项上,而不是出现在主窗口上。
我对 python 很陌生,所以我很感谢知识渊博的人提供的任何帮助。
编辑: 我找到了一个可行的解决方案,删除 Options 类并将 def btnOptionsClick() 更改为:
def btnOptionsClick():
opt = Toplevel(root, takefocus=True)
chkMon = Checkbutton(opt, text="Mon").place(x=50, y=50)
I have a button that pops up an options window, when I try to populate the widgets on the new window they only appear on the parent window.
here is the related code:
from Tkinter import *
class MainWindow:
def __init__(self, master):
"""builds main window."""
windowAttr = {"width":450, "height":150}
window = Frame(master, windowAttr).grid()
btnAttr = {"text":"Options", "width":12, "height":1}
self.btnOptions = Button(window, btnAttr, command=btnOptionsClick).place(x=360, y=5)
class Options:
def __init__(self, optMaster):
"""Builds and displays the options window"""
optAttr = {"width":300, "height":200}
optWin = Frame(optMaster, optAttr).grid()
self.chkMon = Checkbutton(optWin, text="Mon").place(x=50, y=50)
def btnOptionsClick():
opt = Tk(className='Options')
optionsApp = Options(opt)
opt.mainloop()
root = Tk(className='Main Window')
app = MainWindow(root)
root.mainloop()
chkMon appears on MainWindow and Options is always empty, I want chkMon to appear on Options and not on MainWindow.
I'm very new to python so I'm thankful for any help knowledgeable folks have.
edit:
I found a working solution, remove the Options class and change def btnOptionsClick() to:
def btnOptionsClick():
opt = Toplevel(root, takefocus=True)
chkMon = Checkbutton(opt, text="Mon").place(x=50, y=50)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将原始 btnOptionsClick() 函数的第一行设置为 ...
,然后将其余部分保留原样。
Try making the first line of your original btnOptionsClick() function be ...
and then leave the rest as it was.