Seite 1 von 1
frame objekt
Verfasst: Mittwoch 25. April 2007, 12:26
von lost_mind
also wen ich in der Myapp(wx.App) bei OnInit sage zb. frame = Myframe(wx.frame)
wie kan ich dann von einer ganz anderen Klasse die frame ansprechen ?
denn MyApp.frame geht nicht auch My.App.OnInit.frame funktioniert nicht ?
ja ich weis das ich es instanzieren muss das hier ist nur ein Beispiel
zur besseren verständis ein code besipiel
Code: Alles auswählen
class MyFrame(wx.Frame):
pass
class MyApp(wx.App):
pass
class test(self):
pass
Re: frame objekt
Verfasst: Mittwoch 25. April 2007, 13:34
von gerold
lost_mind hat geschrieben:wie kan ich dann von einer ganz anderen Klasse die frame ansprechen?
Hi lost_mind!
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import wx
wx.SetDefaultPyEncoding("iso-8859-15")
class MyFirstFrame(wx.Frame):
def __init__(self, parent = None, id = -1, title = "My First Frame"):
wx.Frame.__init__(self, parent, id, title)
self.app = wx.GetApp()
# Panel
panel = wx.Panel(self)
font = panel.GetFont()
font.SetPointSize(font.GetPointSize() + 2)
panel.SetFont(font)
self.panel = panel
# Main-Sizer
vbox_main = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox_main)
# Label
self.my_label = wx.StaticText(panel, size = wx.Size(200, -1))
self.my_label.SetBackgroundColour("yellow")
vbox_main.Add(self.my_label, 0, wx.EXPAND | wx.ALL, 10)
# Button 1
my_button = wx.Button(panel, -1, "Show second frame")
vbox_main.Add(my_button, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 10)
my_button.Bind(wx.EVT_BUTTON, self.show_second_frame)
# Layout
panel.Fit()
self.Fit()
self.SetSizeHintsSz(self.GetSize())
def show_second_frame(self, event = None):
app = wx.GetApp()
app.show_second_frame()
class MySecondFrame(wx.Frame):
def __init__(self, parent = None, id = -1, title = "My Second Frame"):
wx.Frame.__init__(self, parent, id, title)
self.app = wx.GetApp()
# Panel
panel = wx.Panel(self)
font = panel.GetFont()
font.SetPointSize(font.GetPointSize() + 2)
panel.SetFont(font)
# Main-Sizer
vbox_main = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox_main)
# Button 1
my_button = wx.Button(panel, -1, "Send value to first frame")
vbox_main.Add(my_button, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 10)
my_button.Bind(wx.EVT_BUTTON, self.send_value_to_first_frame)
# Layout
panel.Fit()
self.Fit()
self.SetSizeHintsSz(self.GetSize())
def send_value_to_first_frame(self, event = None):
app = wx.GetApp()
assert isinstance(app, MyApp)
ff = app.my_first_frame
ff.my_label.SetLabel("Hallo!")
ff.panel.Layout()
class MyApp(wx.PySimpleApp):
def OnInit(self):
self.my_first_frame = None
self.my_second_frame = None
self.show_first_frame()
return True
def show_first_frame(self):
if not isinstance(self.my_first_frame, MyFirstFrame):
self.my_first_frame = MyFirstFrame()
self.my_first_frame.Show()
def show_second_frame(self):
if not isinstance(self.my_second_frame, MySecondFrame):
self.my_second_frame = MySecondFrame(self.my_first_frame)
self.my_second_frame.Show()
def main():
app = MyApp()
app.MainLoop()
if __name__ == "__main__":
main()
mfg
Gerold
Edit: Code verändert
Verfasst: Donnerstag 26. April 2007, 12:53
von lost_mind
das heist ich muss die funktionen in der klasse definieren und sie dan aufrufen und ich kann nicht von "ausen auf das frame objekt zugreifen das in wx.app instanziert wurde ?
btw. sry für mein schlecht erklärte problemstellung aber meine mittagspause war fast zu ende

Verfasst: Donnerstag 26. April 2007, 13:29
von gerold
Hi lost_mind!
Ich kann es nicht besser erklären, ohne auf Klasseninstanzen und objektorientierte Programmierung einzugehen. Deshalb habe ich das Bespiel verändert. Vielleicht kannst du damit ja etwas anfangen.
mfg
Gerold

Verfasst: Donnerstag 26. April 2007, 14:01
von lost_mind
ich glaube du verstehst mich nicht ganz was ich meine oder ich versteh dich nicht richtig

deswegen hie rnochmal ein besseres codebeispiel zur verdeutlichung meiner problemstellung
Code: Alles auswählen
class Test(object):
def __init__:(self):
app.frame.funktion # geht nicht
app.OnInit.frame.funktion # geht auch nicht
calss MyFrame(wx.Frame):
pass
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame()
app = MyApp()
programme = Test()
ich möchte ein widget im fram objekt ansprechen aber das geht nicht weil ich nicht weis wie ich auf frame zugreife
ich habs zur zeit mit der global anweisung gelöst ist aber nicht die pythonischte lösung

oder