嘗試在tkinter中繪圖。我設定了一個root視窗,分左右兩個frame,右邊放widget,左邊擬做繪圖區。當選擇執行 fig, axes = plt.subplots(1, 4) 陳述句時,明顯看到root視窗被強制縮小,組件上的文字也變小,似乎解析度加大了。但是呼叫
winfo_screenheight()和
winfo_screenwidth()顯示的始終都是一個數值。請問各位高人,這是什么原因呢?怎么保持解析度不因subplots的呼叫而變化呢?以下是代碼:
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def set_paras():
root.wm_title("股票")
root.geometry("1200x750+10+10")
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['savefig.dpi'] = 100
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (6.4, 4.8)
root.update()
return
root = tk.Tk()
maxx, maxy, right_f, butx, buty1 = 1200, 750, 250, 10, 20
buty2, reph, repw, repy = 70, 13, 32, 180
set_paras()
framel = tk.Frame(root, width=maxx-right_f, height=maxy)
framel.place(x=0, y=0, anchor="nw")
framer = tk.Frame(root, width=right_f, height=maxy, bg="lightcyan")
framer.place(x=maxx-right_f, y=0, anchor="nw")
framel.grid(row=0, column=1, sticky="ns")
but1 = tk.Button(master=framer, text=" Quit ", command=root.quit).place(x=butx+150, y=buty1, anchor="nw")
report1 = tk.Text(master=framer, width=repw, height=reph, bg="#EFEFEF")
report1.place(x=butx, y=maxy-repy, anchor="nw")
report1.insert(tk.INSERT, "something...\n")
fig = plt.Figure()
canvas1 = FigureCanvasTkAgg(fig, framel)
canvas1.get_tk_widget().pack(side="top", fill="both")
### 下面這一句,執行與不執行比,tkinter視窗的解析度似乎加大,視窗和里面內容小了一號 ##################
fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2)
root.mainloop()
uj5u.com熱心網友回復:
剛才又試了, 把 fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2) 改成了 ax = fig.add_subplot(411) 等,不會像subplots一樣影響了tkinter的視窗大小及解析度。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/267211.html
上一篇:IndexError: list index out of range
下一篇:機械研究生
