我想將這兩個元素放置在我視窗的不同極端,因此,標簽將放置在左側,按鈕將放置在右側,如下圖所示

我嘗試了不同風格的布局管理,如包和網格,但無法解決我的問題。
主檔案
from faces.schedules import Schedules
from faces.App import App
app = App()
schedules = Schedules(app)
app.mainloop()
Tkinter 視窗 (app.py)
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.app_width = 800
self.app_height = 600
self.setup()
def setup(self):
self.title('Backup Manager')
self.iconbitmap('images/icon.ico')
#dimensoes
self.resizable(False, False)
self.geometry(newGeometry=f'{self.app_width}x{self.app_height}')
self.style = ttk.Style(self)
self.style.theme_use('xpnative')
時間表.py
from tkinter.ttk import Button, Label, Frame
class Schedules(Frame):
def __init__(self, master):
super().__init__()
self.setupPage()
def setupPage(self):
self.header = Frame(self)
self.title = Label(self.header, text="Meus agendamentos")
self.setPageButton = Button(self.header, text='Mudar')
self.gridElements()
def gridElements(self):
self.header.grid(sticky='we')
self.title.grid(row=0, column=0, sticky='W')
self.setPageButton.grid(row=0, column=1, sticky= 'E')
self.grid()
uj5u.com熱心網友回復:
我建議使用加權列 columnconfigure()
import tkinter as tk
root = tk.Tk()
root.geometry("200x200")
root.columnconfigure(1, weight = 2) # Configures column 1 to function as 2 columns
tk.Button(root, text = "Left") .grid(row = 0, column = 0)
tk.Button(root, text = "Right") .grid(row = 0, column = 1, sticky = tk.E)
root.mainloop()

uj5u.com熱心網友回復:
對于您的情況,我建議使用.pack()而不是.grid():
主檔案
from faces.schedules import Schedules
from faces.app import App
app = App()
schedules = Schedules(app)
schedules.pack(fill='x')
app.mainloop()
時間表.py
from tkinter.ttk import Button, Label, Frame
class Schedules(Frame):
def __init__(self, master):
super().__init__(master)
self.setupPage()
def setupPage(self):
self.header = Frame(self)
self.title = Label(self.header, text="Meus agendamentos")
self.setPageButton = Button(self.header, text='Mudar')
self.packElements()
def packElements(self):
# use pack() instead of grid()
self.header.pack(fill='x')
self.title.pack(side='left')
self.setPageButton.pack(side='right')
uj5u.com熱心網友回復:
您是否嘗試過使用 .place 手動放置它?我使用這樣的東西:
self.setPageButton = Button(self.header, text="Meus agendamentos", command='What this button does if pressed').place(x = x-coord, y = y-coord) here
由于您的應用程式視窗是 800x600,您可能想嘗試將按鈕放置在 (x = 780, y = 0 并通過實驗移動它。自從我使用 tkinter 已經有一段時間了,所以不確定它的效果如何。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383489.html
下一篇:在列樹視圖中提取特定值
