1. 程式人生 > >33-wxpython多個frame之間的資訊共享

33-wxpython多個frame之間的資訊共享

https://blog.csdn.net/xyisv/article/details/78576932

https://blog.csdn.net/tianmaxingkong_/article/details/53326463

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/10/27 15:19
# @Author  : ystraw
# @Site    :
# @File    : Calculator.py
# @Software: PyCharm Community Edition
# @function:

import wx
import math

class Calculator(wx.Frame):
    def __init__(self, update):
        wx.Frame.__init__(self, None, -1, 'ystraw_onw',
                size=(400, 500),  pos=(500, 100))
        self.panel = wx.Panel(self) #建立畫
        self.update = update
        #建立輸入文字框
        self.textprint = wx.TextCtrl(self.panel, wx.NewId(), 'ysong', size = (400, 55),  style=wx.TE_MULTILINE | wx.TE_READONLY)
        #建立button
        button = wx.Button(self.panel, wx.NewId(), 'one' , size=(100, 100), pos = (50 , 100))  # 將按鈕新增到畫板
        self.Bind(wx.EVT_BUTTON, self.exchang, button)

    def exchang(self, event):
        button = event.GetEventObject()
        self.textprint.SetValue('one')
        self.update(2)

class frame2(wx.Frame):
    def __init__(self, update):
        wx.Frame.__init__(self, None, -1, 'ystraw_two',
                size=(400, 500),  pos=(500, 100))
        self.panel = wx.Panel(self) #建立畫
        self.update = update
        #建立輸入文字框
        self.textprint = wx.TextCtrl(self.panel, wx.NewId(), 'yyy', size = (400, 55),  style=wx.TE_MULTILINE | wx.TE_READONLY)
        #建立button
        button = wx.Button(self.panel, wx.NewId(), 'two' , size=(100, 100), pos = (50 , 100))  # 將按鈕新增到畫板
        self.Bind(wx.EVT_BUTTON, self.exchang, button)

    def exchang(self, event):
        button = event.GetEventObject()
        self.textprint.SetValue('two')
        self.update(1)

class MyApp(wx.App):
  def OnInit(self):
    #建立視窗時要講下面的函式一起傳過去,這樣在其它函式裡面才能呼叫公共的函式,起到資訊共享的效果
    self.myframe = Calculator(self.update)
    self.myframe2 = frame2(self.update)
    self.SetTopWindow(self.myframe)
    self.myframe.Show(True)
    self.myframe2.Show(True)
    return True
  #作為多個視窗之間的媒介
  def update(self, type):
      if type == 1:
         self.myframe.Show(False)
      else:
          self.myframe2.Show(False)
if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()