我的目標是創建一個具有垂直和水平滾動的GUI TKinter類。類內的Python Tkinter滾動條
我有一個圖表,其維度大於GUI的窗口維度。我把圖表放在畫布中,以啓用允許垂直和水平滾動的滾動條功能。
但是,當我運行我的代碼時,圖形大小會自動適應GUI窗口的大小,從而扭曲圖形比例並且滾動條功能不起作用。
有沒有人知道這個解決方案?這裏是代碼:
from tkinter import *
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class App(Tk):
def __init__(self):
Tk.__init__(self)
Frame_parent = Frame(self, bg = 'white', borderwidth = 0, relief = FLAT)
Frame_parent.pack(side = TOP, padx = 1, pady = 1)
Canvas_parent = Canvas(Frame_parent, scrollregion = (0, 0, 2000, 2000))
vbar=Scrollbar(Frame_parent, orient = VERTICAL)
vbar.pack(side = RIGHT, fill = Y)
vbar.config(command = Canvas_parent.yview)
hbar=Scrollbar(Frame_parent, orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=Canvas_parent.xview)
Canvas_parent.config(xscrollcommand=hbar.set, yscrollcommand = vbar.set)
Canvas_parent.pack(side = LEFT, expand = True, fill = BOTH)
Frame_child = Frame(Canvas_parent, bg = 'white', borderwidth = 2, relief = FLAT)
Frame_child.pack(side = TOP, padx = 10, pady = 1)
f = Figure(figsize=(6, 5), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
a.plot(t, s)
canvas = FigureCanvasTkAgg(f, Frame_child)
canvas.show()
canvas.get_tk_widget().pack(side = BOTTOM, fill = BOTH, expand = False)
if __name__ == "__main__":
app = App()
app.geometry("400x400+51+51")
app.title("Test")
app.mainloop()
它的工作原理。非常感謝您的反饋和代碼解釋。我對滾動條在tkinter上的工作原理有了更多的瞭解。 – Fxs7576
不客氣。 –