我是新的CustomTkinter和试图添加一个数字到一个框架。我按照网格管理器教程创建了这段代码,并尝试在框架中绘制一个图形。它运行,但不显示情节,只有框架和标题。我错过了什么?
import customtkinter
import tkinter
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import numpy as np
customtkinter.set_appearance_mode("dark") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("green") # Themes: "blue" (standard), "green", "dark-blue"
customtkinter.set_appearance_mode("dark")
class MyFigureFrame(customtkinter.CTkFrame):
def __init__(self, master, title):
super().__init__(master)
self.grid_columnconfigure(0, weight=1)
# self.x = x
# self.x = y
self.title = title
self.title = customtkinter.CTkLabel(self, text=self.title, fg_color="gray30", corner_radius=6)
self.title.grid(row=0, column=0, padx=10, pady=(10, 0), sticky="ew")
fig = Figure()
t = np.arange(0, 3, .01)
fig.add_subplot().plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, self) # A tk.DrawingArea.
canvas.draw()
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.title("my app")
self.geometry("400x480")
self.grid_columnconfigure((0, 1), weight=1)
self.grid_rowconfigure(0, weight=1)
self.MyFigureFrame = MyFigureFrame(self, "Figure")
self.MyFigureFrame.grid(row=0, column=0, padx=10, pady=(10, 0), sticky="nsew")
app = App()
app.mainloop()
1条答案
按热度按时间yduiuuwa1#
绘图不显示在框架内的原因是需要先将画布对象添加到其框架布局中。
在提供的代码中,创建了canvas对象,但没有将其添加到框架的布局中。要将画布对象添加到框架的布局中,可以使用grid()方法指定画布对象应放置的行和列。
为了确保最佳解决方案,您可以在MyFigureFrame类的末尾包含以下代码行。这将向其框架布局添加绘图画布小部件: