![](/img/trans.png)
[英]How to destroy a button widget that was placed in an if condition? Tkinter
[英]How to make Tkinter button to be placed in particular position?
我是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()
當我使用tkinter時,我使用列和行來定位對象
self.btn = tk.Button(self, text = "button")
self.btn.grid(row = 1, column = 1)
編輯-擴展信息以回應評論(下)
我會制作一個標簽並更改其寬度和高度以形成所需的間距(請注意,我也是python的初學者,因此這可能是一種不好的方法,但它可以工作)
from tkinter import *
import tkinter as tk
from tkinter.ttk import Combobox,Treeview,Scrollbar
class MainMenu(Frame):
def __init__(self, master):
""" Initialize the frame. """
super(MainMenu, self).__init__(master)
self.grid()
self.create_GUI()
def create_GUI(self):
frame1 = tk.LabelFrame(self, text="frame1", width=300, height=130, bd=5)
frame1.grid(row=0, column=0, columnspan=3, padx=8)
#the frame is not needed but it is a good thing to use as can group
#parts of your interface together
self.text1 = Entry(frame1)
#note if you were not using frames would just put self here
self.text1.grid(row = 1, column = 0)
self.text2 = Label(frame1, text = "",height = 10)
self.text2.grid(row = 2 , column = 0)
self.text3 = Entry(frame1)
self.text3.grid(row = 3, column = 0)
root = Tk()
root.title("hi")
root.geometry("500x500")
root.configure(bg="white")
app = MainMenu(root)
root.mainloop()
還要注意,不能將打包和網格一起使用,只能將對象分組到不同的幀中,然后在一個幀中使用網格並在另一個幀中打包。 我個人更喜歡使用網格來打包,因為它比打包可以更好地控制對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.