Python 无法使tkinter按钮正确定位 我希望按钮在屏幕的相对侧,但是我不确定为什么它们一直在中间定位。代码是这样设置的,因为我计划使用show\u frame方法在多个覆盖之间切换。类InputWindow就是其中一个覆盖。当我创建一个只有窗口和按钮的基本脚本时,我能够正确地定位按钮,但是我不确定我在这里做了什么错误的事情,这会阻止我正确地远离按钮 import tkinter as tk class GuiController(tk.Tk): def __init__(self, *args , **kwargs): tk.Tk.__init__(self, *args, **kwargs) container = tk.Frame(self) container.pack(side = "top", fill = "both", expand = True) container.grid_rowconfigure(0, weight = 1) container.grid_columnconfigure(0, weight = 1) self.frames = {} frame = InputWindow(container, self) self.frames[InputWindow] = frame frame.pack() self.show_frame(InputWindow) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() class InputWindow(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) controller.geometry("650x500") button_defaultGame = tk.Button(self, text = "Default Game") button_defaultGame.grid(row = 0, column = 0, sticky = "W") button_test = tk.Button(self, text = "Test") button_test.grid(row = 0, column = 1, sticky = "E") self.columnconfigure(0, weight=1) self.columnconfigure(1, weight=2) app = GuiController() app.mainloop()
当与布局问题斗争时,它有助于给你的框架独特的颜色。否则,很难看到一帧的结束和另一帧的开始 例如,为容器指定如下颜色:Python 无法使tkinter按钮正确定位 我希望按钮在屏幕的相对侧,但是我不确定为什么它们一直在中间定位。代码是这样设置的,因为我计划使用show\u frame方法在多个覆盖之间切换。类InputWindow就是其中一个覆盖。当我创建一个只有窗口和按钮的基本脚本时,我能够正确地定位按钮,但是我不确定我在这里做了什么错误的事情,这会阻止我正确地远离按钮 import tkinter as tk class GuiController(tk.Tk): def __init__(self, *args , **kwargs): tk.Tk.__init__(self, *args, **kwargs) container = tk.Frame(self) container.pack(side = "top", fill = "both", expand = True) container.grid_rowconfigure(0, weight = 1) container.grid_columnconfigure(0, weight = 1) self.frames = {} frame = InputWindow(container, self) self.frames[InputWindow] = frame frame.pack() self.show_frame(InputWindow) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() class InputWindow(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) controller.geometry("650x500") button_defaultGame = tk.Button(self, text = "Default Game") button_defaultGame.grid(row = 0, column = 0, sticky = "W") button_test = tk.Button(self, text = "Test") button_test.grid(row = 0, column = 1, sticky = "E") self.columnconfigure(0, weight=1) self.columnconfigure(1, weight=2) app = GuiController() app.mainloop(),python,tkinter,Python,Tkinter,当与布局问题斗争时,它有助于给你的框架独特的颜色。否则,很难看到一帧的结束和另一帧的开始 例如,为容器指定如下颜色: container = tk.Frame(self, bg="bisque") class InputWindow(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent, bg="pink") fram
container = tk.Frame(self, bg="bisque")
class InputWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="pink")
frame.pack(side="top", fill="both", expand=True)
接下来,为您的输入窗口
提供不同的颜色,如下所示:
container = tk.Frame(self, bg="bisque")
class InputWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="pink")
frame.pack(side="top", fill="both", expand=True)
运行代码时,您将看到如下内容:
container = tk.Frame(self, bg="bisque")
class InputWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="pink")
frame.pack(side="top", fill="both", expand=True)
这立即表明,InputWindow
没有填充容器窗口。查看您的代码,我们可以看到您正在执行frame.pack()
将InputWindow的实例添加到容器中
相反,您需要请求InputWindow
填充容器窗口。您可以通过将呼叫更改为pack
,如下所示:
container = tk.Frame(self, bg="bisque")
class InputWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="pink")
frame.pack(side="top", fill="both", expand=True)
现在我们可以看到,InputWindow
的实例填充了容器,您的按钮确实位于窗口的边缘
InputWindow
本身正在缩小以适应其内容-您对按钮所做的任何操作都不会使它们移动,因为没有额外的空间让它们在其中移动。当您.pack()
(或.grid()
)InputWindow
时,您需要告诉它展开以填充根窗口。为什么要调用container.row\u configure(…)
和container.column\u configure(…)
,而使用frame.pack()
?您是否应该使用frame.grid(sticky='nsew')
?