为什么 Tkinter 框架不显示所有三个按钮,除非将 gui 拖到 wider/bigger 大小
Why is a Tkinter frame not showing all three buttons unless gui is dragged into wider/bigger size
我有一个图形用户界面,它没有按照我想要的方式显示按钮。框架正在裁剪,除非 gui 被拉伸。
像这样,我终于看到了想要的三个按钮:
我想让带按钮的框架 (frame2) 始终显示三个按钮并保持相同的大小,无论 gui 放大到什么大小。知道我哪里出错了吗?
代码
import tkinter as tk
import tkinter
from tkinter import ttk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
#=====================================================================
# ROOT FIGURE FOR GUI
#=====================================================================
root = tk.Tk()
root.title("Tab Widget")
root.geometry("600x450")
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='Circle Cal')
tabControl.add(tab2, text ='OPW')
tk.Grid.rowconfigure(root, 0, weight=1)
tk.Grid.columnconfigure(root, 0, weight=1)
tabControl.grid(column=0, row=0, sticky=tk.E+tk.W+tk.N+tk.S)
#MAKE A FIGURE OBJECT
my_figure1 = Figure(figsize = (4, 4), dpi = 100)
#MAKE A FRAME WIDGET
frame1 = tk.Frame(tab1, bd=2, relief=tk.GROOVE)
frame1.pack(side=tk.LEFT, anchor=tk.N, fill=tk.BOTH, expand=True)
#create another frame(frame2)
frame2 = tk.Frame(tab1, bd=2, relief=tk.GROOVE)
frame2.pack(side=tk.RIGHT, anchor=tk.E, fill=tk.BOTH)
#MAKE A CANVAS OBJECT
my_canvas1 = FigureCanvasTkAgg(my_figure1, master = frame1) # creating the Tkinter canvas containing the Matplotlib figure
# TURN THE CANVAS OBJECT INTO A CANVAS WIDGET
my_canvas1.get_tk_widget().pack(side = tkinter.TOP, fill = tkinter.BOTH, expand = 1) # placing the canvas on the Tkinter window
my_canvas1.draw()
def plotData():
pass
def clearPlot():
pass
# MAKE BUTTON TO PLOT GRAPH
button1 = tk.Button(frame2, text = "Plot", command = plotData, relief = tk.GROOVE, padx =20, pady =20 )
button1.grid(row = 0, column = 0)
# MAKE BUTTON TO CLEAR PLOT
button2 = tk.Button(frame2, text = "Clear", command = clearPlot, relief = tk.GROOVE, padx =20, pady =20 )
button2.grid(row = 0, column = 1)
# MAKE BUTTON TO close
button2 = tk.Button(frame2, text = "Close", command = clearPlot, relief = tk.GROOVE, padx =20, pady =20 )
button2.grid(row = 0, column = 2)
root.mainloop()
你试过删除
root = tk.Tk()
root.title("Tab Widget")
#root.geometry("600x450")
几何管理器
答案很简单,没有足够的空间放置按钮。您强制 window 的大小为特定宽度,而宽度太小了。
当您强制 window 为特定尺寸时,pack
将需要缩小一个或多个小部件以使所有小部件适合。它以与添加小部件 pack
.
相反的顺序执行此操作
由于您希望canvas成为可以增长和收缩的小部件,因此您需要最后打包包含它的框架。因此,在 frame1
上调用 pack
之前,先在 frame2
上调用 pack
。
如果您将对 pack
的调用分组在一起而不是将它们与小部件创建交织在一起,这将是最简单的。
frame2.pack(side=tk.RIGHT, anchor=tk.E, fill=tk.BOTH)
frame1.pack(side=tk.LEFT, anchor=tk.N, fill=tk.BOTH, expand=True)
我有一个图形用户界面,它没有按照我想要的方式显示按钮。框架正在裁剪,除非 gui 被拉伸。
像这样,我终于看到了想要的三个按钮:
我想让带按钮的框架 (frame2) 始终显示三个按钮并保持相同的大小,无论 gui 放大到什么大小。知道我哪里出错了吗?
代码
import tkinter as tk
import tkinter
from tkinter import ttk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
#=====================================================================
# ROOT FIGURE FOR GUI
#=====================================================================
root = tk.Tk()
root.title("Tab Widget")
root.geometry("600x450")
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='Circle Cal')
tabControl.add(tab2, text ='OPW')
tk.Grid.rowconfigure(root, 0, weight=1)
tk.Grid.columnconfigure(root, 0, weight=1)
tabControl.grid(column=0, row=0, sticky=tk.E+tk.W+tk.N+tk.S)
#MAKE A FIGURE OBJECT
my_figure1 = Figure(figsize = (4, 4), dpi = 100)
#MAKE A FRAME WIDGET
frame1 = tk.Frame(tab1, bd=2, relief=tk.GROOVE)
frame1.pack(side=tk.LEFT, anchor=tk.N, fill=tk.BOTH, expand=True)
#create another frame(frame2)
frame2 = tk.Frame(tab1, bd=2, relief=tk.GROOVE)
frame2.pack(side=tk.RIGHT, anchor=tk.E, fill=tk.BOTH)
#MAKE A CANVAS OBJECT
my_canvas1 = FigureCanvasTkAgg(my_figure1, master = frame1) # creating the Tkinter canvas containing the Matplotlib figure
# TURN THE CANVAS OBJECT INTO A CANVAS WIDGET
my_canvas1.get_tk_widget().pack(side = tkinter.TOP, fill = tkinter.BOTH, expand = 1) # placing the canvas on the Tkinter window
my_canvas1.draw()
def plotData():
pass
def clearPlot():
pass
# MAKE BUTTON TO PLOT GRAPH
button1 = tk.Button(frame2, text = "Plot", command = plotData, relief = tk.GROOVE, padx =20, pady =20 )
button1.grid(row = 0, column = 0)
# MAKE BUTTON TO CLEAR PLOT
button2 = tk.Button(frame2, text = "Clear", command = clearPlot, relief = tk.GROOVE, padx =20, pady =20 )
button2.grid(row = 0, column = 1)
# MAKE BUTTON TO close
button2 = tk.Button(frame2, text = "Close", command = clearPlot, relief = tk.GROOVE, padx =20, pady =20 )
button2.grid(row = 0, column = 2)
root.mainloop()
你试过删除
root = tk.Tk()
root.title("Tab Widget")
#root.geometry("600x450")
几何管理器
答案很简单,没有足够的空间放置按钮。您强制 window 的大小为特定宽度,而宽度太小了。
当您强制 window 为特定尺寸时,pack
将需要缩小一个或多个小部件以使所有小部件适合。它以与添加小部件 pack
.
由于您希望canvas成为可以增长和收缩的小部件,因此您需要最后打包包含它的框架。因此,在 frame1
上调用 pack
之前,先在 frame2
上调用 pack
。
如果您将对 pack
的调用分组在一起而不是将它们与小部件创建交织在一起,这将是最简单的。
frame2.pack(side=tk.RIGHT, anchor=tk.E, fill=tk.BOTH)
frame1.pack(side=tk.LEFT, anchor=tk.N, fill=tk.BOTH, expand=True)