【发布时间】:2015-08-12 01:23:55
【问题描述】:
我正在创建一个 Tkinter 应用程序,该应用程序在 GUI 中有一个框架,根据用户在程序中所做的更改而有所不同。它似乎可以在显示哪个帧之间切换。但是,每次返回到它所在的框架时,旋转框小部件都会改变其大小。我尝试使用 width=5 使用固定宽度,但仍然遇到同样的问题。我还尝试使用pack() 而不是grid()。是什么导致它改变形状,我该如何阻止它?
import tkinter as tk
from tkinter import ttk
class ODE_Solver_App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
equation_settings = ttk.Frame(self)
equation_settings.pack(side="top", fill="both", expand = True)
equation_settings.grid_rowconfigure(0, weight=1)
equation_settings.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (ODE_Equation_Settings, System_Equation_Settings):
frame = F(equation_settings, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(ODE_Equation_Settings)
# Buttons in the main class for switching between frames.
button1 = tk.Button(self, text="ODE Settings", command=lambda: self.show_frame(ODE_Equation_Settings))
button1.pack()
button2 = tk.Button(self, text="System Settings", command=lambda: self.show_frame(System_Equation_Settings))
button2.pack()
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class ODE_Equation_Settings(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
equation_profile = tk.Frame(self) # I am going to have more frames in this class.
equation_profile.pack()
order_label = tk.Label(equation_profile, text="Order:")
order_label.grid(row=0, column=0, sticky="w")
order_select = tk.Spinbox(equation_profile, width=5, from_=1, to=100000) # This is the spinbox that is changing in size.
order_select.grid(row=0, column=1, sticky="e")
class System_Equation_Settings(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is Another Page!")
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="This does nothing yet...")
button1.pack()
app = ODE_Solver_App()
app.mainloop()
我在 Yosemite 上使用 Python 3.4
编辑: 我尝试在不同的计算机上运行该程序。结果如下: Mac OS Mavericks - 完全相同的问题。 Windows XP(mac 上的虚拟机)- 工作得很好。
问题似乎与我正在使用的操作系统有关。
【问题讨论】:
-
不知何故,我系统上的小部件在切换框架后不会改变大小。您的 Spinbox 是如何变化的?是变大还是变小?
-
它越来越小了。当我运行它时,看起来宽度好像变小了。最终它会完全消失。此外,标签“订单:”似乎被某些东西(可能不再可见的小部件?)覆盖并且也消失了。
-
似乎适用于我的系统(windows 7 64bit,Python 2.7)
-
我刚刚在 Windows XP 上尝试过,它可以工作。但它不适用于我的任何 Mac。 mac 上的 tkinter 可能存在错误?
标签: python user-interface tkinter widget