【发布时间】:2016-07-19 20:50:33
【问题描述】:
我是 python 新手,所以我试图制作一个 GUI,因为我必须在特定位置放置一个按钮。
我尝试使用self.nxt_form.place(x=200,y=100) 而不是self.nxt_form.pack()。
但是按钮消失了,运行时只出现了框架。您能告诉我如何将按钮放置在特定位置吗?
代码如下:
import tkinter as tk
class Main_form:
def __init__(self, root,title="Simulated MTBF"):
self.root = root
self.frame = tk.Frame(self.root)
"""Button nxt_form which moves to next form"""
self.nxt_form = tk.Button(self.frame, text = 'Next Form', width = 25,command = self.new_window)
self.nxt_form.pack()
self.frame.pack()
"""command to open new window by clicking Button """
def new_window(self):
self.newWindow = tk.Toplevel(self.root)
self.app = Demo2(self.newWindow)
class Demo2:
def __init__(self, root):
self.root = root
self.frame = tk.Frame(self.root)
self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
self.quitButton.pack()
self.frame.pack()
def close_windows(self):
self.root.destroy()
def main():
root = tk.Tk()
app = Main_form(root)
root.mainloop()
if __name__ == '__main__':
main()
【问题讨论】:
-
需要放在什么“特定位置”? 200,100有什么神奇之处?避免使用
place通常是个好主意,因为它不能很好地处理不同的字体、不同的分辨率或不同的窗口大小(除非你完全了解它的所有选项)。
标签: python eclipse user-interface python-3.x tkinter