Ich habe ein Problem mit Matplotlib in Tkinter. Hiermatplotlib und tkinter interactive: Wie wählt man den definierten Frame in Tkinter?
ist der Plan:
1. Definieren Sie eine Button
und ein Frame
(Frame_0) mit Tkinter.
2. Klicken Sie auf Button
, um die Sin-Funktion mit matplotlib
zu plotten, die in Frame_0
angezeigt wird.
Aber ich weiß nicht, wie es geht.
Ich bin nur ein Anfänger. Aber ich denke nur, der einfachste Weg, es zu tun, "Wählen" das Widget mit get_tk_widget()
, ist es möglich? Oder gibt es andere Befehle, um das zu tun?
Ich hoffe, Sie verstehen meine schlechte Englisch und meine Anfänger Frage. Hier ist der Code. Danke euch allen.
# -*- coding: utf-8 -*-
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
def command_0():
f = Figure(figsize=(5, 3), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2 * pi * t)
a.plot(t, s)
dataPlot = FigureCanvasTkAgg(f, master=root)
dataPlot.show()
dataPlot.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
root.mainloop()
def Main():
global root
root = Tk()
root.title("Program")
root['background']='gray'
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
root.geometry("%dx%d" %(w, h))
root.state("zoomed")
global Frame_0
Frame_0 = Frame(root)
Frame_0.place(height=600, width=800, x=10, y=10)
Frame_1 = Frame(root)
Frame_1.place(width=120, height=80, x=1000, y=20)
Button_1 = Button(Frame_1, text = "Click", width = 120, height=80, bg='green', command = command_0)
Button_1.place(x=1000, y=20)
Button_1.grid(row=0, column=0)
Button_1.pack()
root.mainloop()
return
Main()
Vielen Dank! –