我有一个图形用户界面,它没有按照我想要的方式显示按钮。除非 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()
答案很简单,没有足够的空间容纳按钮。您强制窗口的大小为特定宽度,但宽度太小了。
当您强制窗口为特定大小时,
pack
将需要缩小一个或多个小部件以使所有小部件适合。它以与使用 pack
添加小部件相反的顺序执行此操作。
由于您希望画布成为可以放大和缩小的小部件,因此您需要最后打包包含它的框架。因此,请先拨打
pack
拨打 frame2
,然后再拨打 pack
拨打 frame1
。
如果您将对
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)
您尝试过删除吗
root = tk.Tk()
root.title("Tab Widget")
#root.geometry("600x450")
几何管理器
尝试全屏
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))