我有两个GUI,我希望这些GUI能够一起通信。我过去使用过Matlab,在Matlab中,我使用过addlistener,基本上可以在多个GUI之间进行通信。我是python的新手,我想在第二个gui上单击显示按钮时更新第一个gui上的轴。基本上,根据我在另一个GUI上选择的路径在另一个gui上绘制图像。
Here is the image for better understanding
代码如下:
from tkinter import *
from PIL import Image, ImageTk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import
FigureCanvasTkAgg
import PySimpleGUI as sg
import tkinter.filedialog as fdialog
from natsort import natsorted
import os
import cv2
class MyCanvas(Canvas):
def __init__(self, parent=None, img=None, *parms, **kparms):
Canvas.__init__(self, parent, *parms, **kparms)
self._width = 20;
self._height = 10;
self._starting_drag_position = ()
self.config(width=self._width, height=self._height, bg='white')
self._draw_some_example_objects()
self.pack(fill=BOTH, expand=YES)
def _draw_some_example_objects(self):
self.fig = Figure()
gs = self.fig.add_gridspec(5, 2)
self.axis= self.fig.add_subplot(gs[0:4, 0])
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.get_tk_widget().pack(side="top", fill='both', expand=True)
colors = dict(outline="black")
class MyGUI(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.title("Drag canvas with mouse")
self.geometry("700x700")
"""For some reason menu should be added here"""
self.menuBar = Menu(master=self)
self.filemenu = Menu(self.menuBar, tearoff=0)
self.filemenu.add_command(label="listview!", command=self.list)
self.menuBar.add_cascade(label="File", menu=self.filemenu)
self.config(menu=self.menuBar)
self._addWidgets()
def _addWidgets(self):
my_canvas = MyCanvas(self)
def list(self):
listView(self)
def listView(self):
sg.ChangeLookAndFeel('GreenTan')
dir = fdialog.askdirectory()
filesList = os.listdir(dir)
filesList = natsorted(filesList)
layout = [
[sg.Listbox(values=(filesList), size=(60, 30), key='_IN_')],
[sg.Button('Show')]
]
window = sg.Window('Everything bagel', default_element_size=(40, 1), grab_anywhere=False).Layout(layout)
while True:
event, values = window.Read()
if event is None or event == 'Exit':
break
print(dir + values.get('_IN_')[0])
if __name__ == '__main__':
MyGUI().mainloop()`
答案 0 :(得分:0)
看看这个demo program。
将您的代码放到显示以下内容的部分:
#------------------------ PASTE YOUR MATPLOTLIB CODE HERE ----------------------
确保图形位于变量“ fig”中。它将创建一个带有GUI选项的窗口,并将您的Matplotlib图嵌入其中。