【问题标题】:How do I create nested windows in python Tkinter?如何在 python Tkinter 中创建嵌套窗口?
【发布时间】:2016-01-17 05:16:21
【问题描述】:

我想在一个窗口内创建一个窗口。外部窗口只是普通的,内部窗口是一个图形。原因是我想制作一个包含多个图表的监控系统,其中嵌套窗口会有所帮助。

代码如下:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import Tkinter

x = []
y = []

top = Tkinter.Tk()

top.title("Hello tkInter");
top.geometry("1000x1000")

fig=plt.figure()

rect = fig.patch
rect.set_facecolor('#31312e')

readFile = open('sampleCSV.csv','r')
sepFile = readFile.read().split('\n')
readFile.close()

for plotPair in sepFile:
    xAndY = plotPair.split(',')
    if xAndY[0] != '':
        x.append(int(xAndY[0]))
        y.append(int(xAndY[1]))

ax1 = fig.add_subplot(2,2,1, axisbg='grey')
ax1.plot(x, y, 'c', linewidth=3.3)
ax1.tick_params(axis='x', colors='c')
ax1.tick_params(axis='y', colors='c')
ax1.spines['bottom'].set_color('w')
ax1.spines['top'].set_color('w')
ax1.spines['left'].set_color('w')
ax1.spines['right'].set_color('w')
ax1.yaxis.label.set_color('c')
ax1.xaxis.label.set_color('c')
ax1.set_title('Matplotlib graph', color = 'c')
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')

ax2 = fig.add_subplot(2,2,2, axisbg='grey')
ax2.plot(x, y, 'c', linewidth=3.3)
ax2.tick_params(axis='x', colors='c')
ax2.tick_params(axis='y', colors='c')
ax2.spines['bottom'].set_color('w')
ax2.spines['top'].set_color('w')
ax2.spines['left'].set_color('w')
ax2.spines['right'].set_color('w')
ax2.yaxis.label.set_color('c')
ax2.xaxis.label.set_color('c')
ax2.set_title('Matplotlib graph', color = 'c')
ax2.set_xlabel('x axis')
ax2.set_ylabel('y axis')

ax3 = fig.add_subplot(2,1,2, axisbg='grey')
ax3.plot(x, y, 'c', linewidth=3.3)
ax3.tick_params(axis='x', colors='c')
ax3.tick_params(axis='y', colors='c')
ax3.spines['bottom'].set_color('w')
ax3.spines['top'].set_color('w')
ax3.spines['left'].set_color('w')
ax3.spines['right'].set_color('w')
ax3.yaxis.label.set_color('c')
ax3.xaxis.label.set_color('c')
ax3.set_title('Matplotlib graph', color = 'c')
ax3.set_xlabel('x axis')
ax3.set_ylabel('y axis')

plt.show()

fig.pack()

top.mainloop()

我无法找到解决方案。请帮忙!

【问题讨论】:

  • Window inside window的可能重复
  • 谢谢史蒂文。这肯定会有所帮助!
  • 如果您想再次查看,我已经更新了上述问题的代码。

标签: python user-interface matplotlib graph tkinter


【解决方案1】:

请阅读good SO questions。代码太多,没有错误回溯,毫无疑问。但是,我相信您的问题是 matplotlib 和 tkinter 不兼容,至少在没有大量工作的情况下并非如此。如果 plt.show() 实际运行,我希望由于 figmatlab.pyplot.figure()Figure 实例,并且该类没有 tkinter pack 方法,fig.pack() 会引发 AttributeError。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多