我有一个问题,将一个图表垂直放置在滚动条中-这个想法是为了制作一个简单的界面来处理数据,有一个窗口,其中包含参数的文本字段和一个按钮来计算结果,并将所有图表和表格与数学估计一起放置。
我的问题是:如何将图表垂直放置在滚动条中(一个在另一个下面)?或者可能有一个更好的主意,在tkinter中放置多个图表(最多20个)而不将它们添加到滚动条中?x1c 0d1x
下面是我的代码:
# importing tkinter gui
import tkinter as tk
from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# import functions
def build_graph(root):
figure3 = plt.Figure(figsize=(5, 4), dpi=100)
ax3 = figure3.add_subplot(111)
x = [i for i in range(100)]
points = [i for i in range(100)]
ax3.plot(x, points, color='b')
scatter3 = FigureCanvasTkAgg(figure3, root)
scatter3.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
ax3.legend(['average voltage difference'])
ax3.set_xlabel('measurement points')
ax3.set_ylabel('voltage(V)')
ax3.set_title('Chart')
# creating window
window = tk.Tk()
# getting screen width and height of display
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
# setting tkinter window size
window.geometry("%dx%d" % (width, height))
window.title("Interface")
label = tk.Label(window, bg='lightblue', text="charts") # creating a label
label.pack() # showing label
# Create A Main frame
top_frame = Frame(master=window) # making an empty container for properties, buttons and fields
top_frame.pack(fill=BOTH,expand=1, side=TOP) # what is fill, expand, side?
# Create Frame for X Scrollbar
top_frame_first = LabelFrame(top_frame, text="Properties")
top_frame_first.pack(fill=X,side=TOP)
# making a new field
frames_number_label = Label(top_frame_first, bg='lightblue', text="enter frames quantity")
frames_number_label.pack(side = LEFT)
frames_number_box = Entry(top_frame_first, width=5)
frames_number_box.insert(0, "1")
frames_number_box.pack(side = LEFT)
path_label = Label(top_frame_first, bg='lightblue', text="write path to file")
path_label.pack(side=LEFT)
path_box = Entry(top_frame_first)
path_box.insert(0, 'experimental.txt')
path_box.pack(side=LEFT)
# Create A Main frame
main_frame = Frame(window)
main_frame.pack(fill=BOTH,expand=1)
# Create Frame for X Scrollbar
sec = Frame(main_frame)
sec.pack(fill=X,side=BOTTOM)
# Create A Canvas
my_canvas = Canvas(main_frame)
my_canvas.pack(side=LEFT,fill=BOTH,expand=1)
# # Add A Scrollbars to Canvas
y_scrollbar = Scrollbar(main_frame,orient=VERTICAL,command=my_canvas.yview)
y_scrollbar.pack(side=RIGHT,fill=Y)
# Configure the canvas
my_canvas.configure(yscrollcommand=y_scrollbar.set)
my_canvas.bind("<Configure>",lambda e: my_canvas.config(scrollregion= my_canvas.bbox(ALL)))
# Create Another Frame INSIDE the Canvas
second_frame = Frame(my_canvas)
# Add that New Frame a Window In The Canvas
my_canvas.create_window((0,0),window= second_frame, anchor="nw")
listbox = Listbox()
# vertical scrollbar
scrollbar = Scrollbar(orient="vertical", command = listbox.yview)
def build():
build_graph(second_frame)
main_button = Button(top_frame, text="Calculate", width=15, height=3)
main_button.config(command=build)
main_button.pack()
window.mainloop() # showing main window
我试着垂直放置一些其他元素-比如按钮,一切都很好,但是当涉及到通过按下按钮在滚动条中垂直放置图表时,它就变得更有挑战性了。
1条答案
按热度按时间rekjcdws1#
您应该使用
side=tk.BOTTOM
选项垂直打包嵌入的Matplotlib画布,并在向画布添加新小部件时重新配置容器画布的scrollregion
选项。所以,这样做。