【问题标题】:Want to open a tkinter window simultaneously with a matplotlib window?想要同时打开一个 tkinter 窗口和一个 matplotlib 窗口吗?
【发布时间】:2016-10-08 14:28:17
【问题描述】:

'''我的代码打开了 matplotlib 窗口和 tkinter,但是 tkinter 窗口在退出 matplotlib 窗口后打开。我希望他们两个同时弹出。'''

from pylab import *
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
import pylab
style.use('seaborn-white')

x = [0,8,-3,-8]
y = [0,8,1,-8]
color=['w','w','w','w']

fig = plt.figure()
ax1 = fig.add_subplot(111)

scatter(x,y, s=100 ,marker='.', c=color,edgecolor='w')
plt.ylabel('X')
plt.xlabel('Y')
ax1.yaxis.label.set_rotation(0)
circle1=plt.Circle((0,0),5,color='r',fill=False,linewidth='4')
fig = plt.gcf()
fig.gca().add_artist(circle1)
left,right = ax1.get_xlim()
low,high = ax1.get_ylim()
arrow( left, 0, right -left, 0, length_includes_head = True, head_width = 0.15 )
arrow( 0, low, 0, high-low, length_includes_head = True, head_width = 0.15 )


fig = pylab.gcf()
fig.canvas.set_window_title('Inner Slip Ring')


grid()
show()

from tkinter import *
root=Tk()
w=Label(root, text="hello")
w.pack()
root.mainloop()

【问题讨论】:

    标签: python matplotlib tkinter


    【解决方案1】:

    把所有东西都放在show()之前:

    ...
    root=Tk()
    w=Label(root, text="hello")
    w.pack()
    #root.mainloop()    # Don't use this
    
    grid()
    show()
    

    【讨论】:

    • 谢谢它的工作,但你能告诉我为什么不使用 root.mainloop
    • 如果您启动主循环,您的程序将在该行停止。它将等到根被销毁(当您关闭窗口时)。 mainloop 只是一个检查事件的无限循环,您只能在应用程序中调用它一次(您使用 show() 这样做)。
    猜你喜欢
    • 2018-05-11
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2015-09-13
    • 2016-03-06
    相关资源
    最近更新 更多