如何使用網格系統更改按鈕的位置?
我讓它在沒有課的情況下作業。
當我運行程式時,它會忽略網格設定...
我想使用網格系統,但它不起作用。
這是它現在的樣子:

但我想要這樣的東西:

提前致謝
import os
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showerror
from tkinter.filedialog import askdirectory
class Frame(ttk.Frame):
def __init__(self, container):
self.options = {'padx': 5, 'pady': 5}
super().__init__(container)
self.grid(padx=5, pady=5, sticky=tk.NSEW)
class Button(Frame):
def __init__(self, container, text, col, row, opt):
super().__init__(container)
self.path = tk.StringVar()
self.button = ttk.Button(self, text=text)
self.button['command'] = self.selectpath
# HERE
self.button.grid(column=col, row=row, sticky=tk.W, **opt)
def selectpath(self):
path_ = tk.filedialog.askdirectory()
self.path.set(path_)
class App(tk.Tk):
def __init__(self):
super().__init__()
tk_width = 330
tk_height = 150
self.geometry(str(tk_width) 'x' str(tk_height))
x_left = int(self.winfo_screenwidth() / 4 - tk_width / 2)
y_top = int(self.winfo_screenheight() / 2 - tk_height / 2)
self.geometry(' {} {}'.format(x_left, y_top))
self.resizable(False, False)
if __name__ == "__main__":
app = App()
window = Frame(app)
selectbutton = Button(window, 'hello', 2, 0, window.options)
selectbutton = Button(window, '1hello', 1, 1, window.options)
app.mainloop()
uj5u.com熱心網友回復:
您使用grid了錯誤的小部件。你把 eachttk.Button放在 new 中,Button(Frame)所以self.button.grid在Button(Frame)eachttk.Button中創建新網格,它放在這個新框架中,而不是放在 main 的主網格中Frame。因為ttk.Button這個網格中只有一個,所以它顯示在同一個地方。網格中的空行和列沒有寬度和高度。
每一個都沒有Button(Frame)使用,所以它把每一個Frame`。self.gridcolumn=row=Button(Frame) in new row in main
你將不得不使用column=,row=以self.grid代替self.button.grid
# put `ttk.Button` in current `Button(Frame)`
self.button.pack()
# put current `Button(Frame)` in main `Frame`
self.grid(column=col, row=row, sticky=tk.W, **opt)
最小作業代碼:

import os
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showerror
from tkinter.filedialog import askdirectory
class Frame(ttk.Frame):
def __init__(self, container):
self.options = {'padx': 5, 'pady': 5}
super().__init__(container)
self.grid(padx=5, pady=5, sticky=tk.NSEW)
class Button(Frame):
def __init__(self, container, text, col, row, opt):
super().__init__(container)
self.path = tk.StringVar()
self.button = ttk.Button(self, text=text)
self.button['command'] = self.selectpath
# put `ttk.Button` in current `Button(Frame)`
self.button.pack()
# put current `Button(Frame)` in main `Frame`
self.grid(column=col, row=row, sticky=tk.W, **opt)
def selectpath(self):
path_ = tk.filedialog.askdirectory()
self.path.set(path_)
class App(tk.Tk):
def __init__(self):
super().__init__()
tk_width = 330
tk_height = 150
self.geometry(str(tk_width) 'x' str(tk_height))
x_left = int(self.winfo_screenwidth() / 4 - tk_width / 2)
y_top = int(self.winfo_screenheight() / 2 - tk_height / 2)
self.geometry(' {} {}'.format(x_left, y_top))
self.resizable(False, False)
if __name__ == "__main__":
app = App()
window = Frame(app)
selectbutton = Button(window, 'hello' , 2, 0, window.options)
selectbutton = Button(window, '1hello', 1, 1, window.options)
app.mainloop()
編輯:
如果你用 createButton(ttk.Button)而不是,也許你就不會遇到這個問題Button(Frame)
class Button(ttk.Button):
def __init__(self, container, text, col, row, opt):
super().__init__(container, text=text, command=self.selectpath)
self.path = tk.StringVar(self)
self.grid(column=col, row=row, sticky=tk.W, **opt)
def selectpath(self):
path = tk.filedialog.askdirectory()
if path:
self.path.set(path)
import os
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showerror
from tkinter.filedialog import askdirectory
class Frame(ttk.Frame):
def __init__(self, master):
super().__init__(master)
self.options = {'padx': 5, 'pady': 5}
self.grid(padx=5, pady=5, sticky=tk.NSEW)
class Button(ttk.Button):
def __init__(self, master, text, col, row, opt):
super().__init__(master, text=text, command=self.selectpath)
self.path = tk.StringVar(self)
self.grid(column=col, row=row, sticky='w', **opt)
def selectpath(self):
path = tk.filedialog.askdirectory()
if path:
self.path.set(path)
class App(tk.Tk):
def __init__(self):
super().__init__()
tk_width = 330
tk_height = 150
x_left = int(self.winfo_screenwidth() / 4 - tk_width / 2)
y_top = int(self.winfo_screenheight() / 2 - tk_height / 2)
self.geometry(f'{tk_width}x{tk_height} {x_left} {y_top}')
self.resizable(False, False)
if __name__ == "__main__":
app = App()
window = Frame(app)
selectbutton = Button(window, 'hello' , 2, 0, window.options)
selectbutton = Button(window, '1hello', 1, 1, window.options)
app.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398966.html
