繁体   English   中英

如何制作代码,在 tkinter 框架的一半和另一半的 plot 上显示按钮?

[英]How can I make a code that displays buttons on one half of a tkinter frame and a plot on the other half?

我这里有一个代码。

import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import matplotlib
import math
import numpy as np

root = tk.Tk()

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
x = np.linspace(3,-3,10000)
p = np.sin(x)
w = np.sin(2*x)
l = np.sin(x)/x
c = np.cos(1/x) 
r = np.tan(x)
ur = np.cos(x)/x
def firstx():
    plt.plot(x, p, 'p-', label = 'y=sin(x)')
    fig.canvas.draw()
def snx():
    plt.plot(x, w, 'c-', label = 'y=sin(2x)')
    fig.canvas.draw()
def thx():
    plt.plot(x, l, 'm-', label = 'y=sin(x)/x')
    fig.canvas.draw()
def fthx():
    plt.plot(x, c, 'v-', label = 'y=sin(1/x)')
    fig.canvas.draw()
def tan():
    plt.plot(x, r, 's-', label = 'y=tan(x)')
    ax.set_ylim([-5, 5])
    fig.canvas.draw()

canvas = FigureCanvasTkAgg(fig, master=root)
plot_widget = canvas.get_tk_widget()

plot_widget.grid(row=0, column=1)

tk.Button(root, text="y=sin(x)", command=firstx).grid(row=0, column=0)
tk.Button(root, text="y=sin(2x)", command=snx).grid(row=1, column=0)
tk.Button(root, text="y=sin(x)/x", command=thx).grid(row=2, column=0)
tk.Button(root, text="y=sin(1/x)", command=fthx).grid(row=3, column=0)
tk.Button(root, text="y=tan(x)", command=tan).grid(row=4, column=0)

root.mainloop()

如果您运行代码,您将在左侧看到一个按钮,在左下方看到按钮的 rest。 如何使按钮全部在左侧而不是左下角?

PS 我正在使用 python 3.6.6。

更改此行:

plot_widget.grid(row=0, column=1)

plot_widget.grid(row=0, column=1, rowspan=5)

Read about the rowspan= argument here https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/grid.html or https: //effbot.org/tkinterbook/grid.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM