在tkinter上创建canvas放置图形

import tkinter as tk
import tkinter.font as tf

from matplotlib.backends._backend_tk import NavigationToolbar2Tk

import python1
from matplotlib.pylab import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

#创建一个window窗口
window = tk.Tk()
window.title(“My Window”)

#获取当前屏幕的宽度、高度
window_width = int(window.winfo_screenwidth()*0.95)
window_height = int(window.winfo_screenheight()*0.95)

#设置window窗口的尺寸
window.geometry(str(window_width) +‘x’+ str(window_height))

#设置字体标签
ft1 = tf.Font(family=‘行楷’,size=30,slant=tf.ITALIC,weight=tf.BOLD)
ft2 = tf.Font(family=‘行楷’,size=20,weight=tf.BOLD)

#设置标签1
l1 = tk.Label(window,text=‘xxxxx’,fg=‘black’,bg=‘red’,font=ft1)
l1.place(relx=0,rely=0,width=window_width,height=window_height0.1)
l2 = tk.Label(window,text=‘xxxx’,fg=‘black’,font=ft2)
l2.place(relx=0.25,rely=0.1,width=window_width
0.6,height=window_height*0.1)
l3 = tk.Label(window,text=‘xxxx’,fg=‘black’,bg=‘yellow’,font=ft2)
l3.place(relx=0,rely=0.2,width=200,height=40)

#定义函数 draw_line_chart,用于画图。接收参数为两个列表数据用于x轴,y轴,返回参数为 图形fig。
def draw_line_chart(x,y):
fig = Figure(figsize=(5,3),dpi=100,edgecolor=‘red’)
ax1 = fig.add_subplot(111)
ax1.plot(x,y,color=“goldenrod”,linewidth=1.5,linestyle=’-’,label=‘Jolon income’, marker=’*’)
# 添加代码块,正常显示中文。
plt.rcParams[‘font.sans-serif’] = [‘SimHei’]
plt.rcParams[‘axes.unicode_minus’] = False # 用于显示符号
ax1.set_xlabel(‘页面’) #设置横坐标轴标题
ax1.set_ylabel(‘时间’) #设置纵坐标轴标题
return fig

#定义函数 generate_canvas_graphics,用于将 draw_line_chart 画的图展示在tkinter上创建的canvas画布上。
def generate_canvas_graphics(figNmae):
# 将绘制的图形显示到tkinter:创建属于window的canvas画布,并将图f置于画布上。
canvas = FigureCanvasTkAgg(figNmae,master=window)
canvas.draw()
canvas.get_tk_widget().place(relx=0.1, rely=0.2, width=window_width * 0.9, height=window_height * 0.8)
# toolbar = NavigationToolbar2Tk(canvas,window)
# toolbar.update()
# canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# frame = tk.Frame(canvas)
# frame.place(relx=0, rely=0, width=window_width, height=window_height)
# hbar = tk.Scrollbar(canvas, bg=‘green’, orient=tk.HORIZONTAL)
# hbar.place(relx=0.2, rely=0.95, width=window_width * 0.7, height=15)
# hbar.configure(command=canvas.xview)

#定义函数 main,用于调用所有函数
def main(x_axis_start_cell,x_axis_end_cell,y_axis_start_cell,y_axis_end_cell):
#调用python2 文件中的main函数,入参为指定区域,出参为 x轴数据,y轴数据。
x_axis_data,y_axis_data = python1.main(x_axis_start_cell,x_axis_end_cell,y_axis_start_cell,y_axis_end_cell)
fig=draw_line_chart(x_axis_data,y_axis_data)
generate_canvas_graphics(fig)

#定义函数 button_event,创建5个按钮,绑定事件。
def button_event():
b1 = tk.Button(window, text=‘AAA’, font=ft2, command=lambda: main(‘A2’,‘A141’,‘C2’, ‘C141’))
b1.place(relx=0, rely=0.3, width=150, height=40)
b2 = tk.Button(window, text=‘BBB’, font=ft2, command=lambda: main(‘A2’,‘A141’,‘D2’, ‘D141’))
b2.place(relx=0, rely=0.4, width=150, height=40)
b3 = tk.Button(window, text=‘CCC’, font=ft2, command=lambda: main(‘A2’,‘A141’,‘E2’, ‘E141’))
b3.place(relx=0, rely=0.5, width=150, height=40)
b4 = tk.Button(window, text=‘DDD’, font=ft2, command=lambda: main(‘A2’,‘A141’,‘F2’, ‘F141’))
b4.place(relx=0, rely=0.6, width=150, height=40)
b5 = tk.Button(window, text=‘EEE’, font=ft2, command=lambda: main(‘A2’,‘A141’,‘G2’, ‘G141’))
b5.place(relx=0, rely=0.7, width=150, height=40)

if name == ‘main’:
button_event()
window.mainloop()

你可能感兴趣的:(在tkinter上创建canvas放置图形)