Problem beim Holen vom letzten Objekt auf dem Fokus war.
Verfasst: Sonntag 21. Januar 2007, 14:12
Ich habe diesen kleinen Vokabeltrainer( fuer Tuerkisch) als Uebung fuer wx programmiert, stehe aber gerade etwas beim Fokus an, ich mag in self.lastfocus speichern auf welchem Widget vorher der Fokus war, danke im Vorraus fuer euere Hilfe!
PS. print "changefocus" wird nie ausgefuehrt.
Der Code mit hervorhebung der wichtigen Zeilen: Pastebin
PS. print "changefocus" wird nie ausgefuehrt.
Der Code mit hervorhebung der wichtigen Zeilen: Pastebin
Code: Alles auswählen
# -*- coding: utf-8 -*-
import wx
import os
import random
if os.name == "posix":
newline = "\n"
elif os.name == "nt":
newline = "\r\n"
#import convert
class MyDialog(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(800, 600))
#self.CreateStatusBar()
self.outputt = wx.StaticText(self, -1, 'LALA!', (410, 300))
#self.outputn = wx.TextCtrl(self, -1, '', (150, 170))
self.given = wx.TextCtrl(self, -1, '', (400, 270), (150, -1))
self.given.SetValue("Turkisch")
self.asked = wx.TextCtrl(self, -1, '', (250, 270), (150, -1))
self.asked.SetValue("Deutsch")
self.add1 = wx.TextCtrl(self, -1, '', (250, 400), (150, -1))
self.add1.SetValue("Turkisch")
self.add = wx.TextCtrl(self, -1, '', (400, 400), (150, -1))
self.add.SetValue("Deutsch")
self.given.SetFocus()
self.G = wx.Button(self, 1, u'ğ', (50, 300))
#compute_btn.SetFocus()
self.I = wx.Button(self, 2, u'ı', (50, 325))
#compute_btn.SetFocus()
self.S = wx.Button(self, 4, u'ş', (50, 350))
self.C = wx.Button(self, 5, u'ç', (50, 375))
self.addVocButton = wx.Button(self, 6, 'Add Vocabulary', (550, 400))
self.submit = wx.Button(self, 7, 'Submit', (550, 270))
self.clear_btn = wx.Button(self, 3, 'Close', (20, 20))
self.widgets = [self.given,self.asked,
self.add1,self.add,
self.G, self.I,
self.S, self.C,
self.addVocButton,self.submit,
self.clear_btn]
for widget in self.widgets:
self.Bind(wx.EVT_SET_FOCUS, self.ChangeFocus, widget)
self.Bind(wx.EVT_BUTTON, self.OnG, self.G)
self.Bind(wx.EVT_BUTTON, self.OnI, self.I)
self.Bind(wx.EVT_BUTTON, self.OnClose, self.clear_btn)
self.Bind(wx.EVT_BUTTON, self.OnS, self.S)
self.Bind(wx.EVT_BUTTON, self.AddVoc, self.addVocButton)
self.Bind(wx.EVT_BUTTON, self.Submit, self.submit)
#wx.EVT_PAINT(self, self.OnPaint)
self.getRandVoc()
#self.Bind(wx.EVT_BUTTON, self.AddVoc, id=4)
self.Bind(wx.EVT_CLOSE, self.OnClose)
def ChangeFocus(self, event):
print "changefocus"
self.lastfocus = event.GetWindow()
#def OnPaint(self, event): # Sets the BG Image
#dc = wx.PaintDC(self)
#dc.DrawBitmap(wx.Bitmap("turkvok.jpg",wx.BITMAP_TYPE_JPEG),0,0)
#event.Skip()
def Submit(self, event):
print self.right
self.given.GetValue()
if self.given.GetValue() == self.right:
self.outputt.SetLabel("Right")
print self.right
print "right"
else:
self.outputt.SetLabel(self.right)
#self.outputt.Set
#print "Richtig"
self.getRandVoc()
def getRandVoc(self, event=None):
read = open ("vocs").readlines()
intr = random.randint(0,1)
if intr == 1:
other = 0
else:
other = 1
splitted = random.sample(read,1)[0][:-1].split("-")
self.asked.SetValue(splitted[intr])
self.right = splitted[other]
def AddVoc(self, event):
print "executing addvoc"
cur = "%s-%s" % (self.add1.GetValue(),self.add.GetValue())
read = open ("vocs").readlines()
for line in read:
print line
print cur
if cur == line[:-1]:
print cur
print line
return True
if os.path.exists("vocs"):
file = open("vocs","a")
else:
file = open("vocs","w")
file.write("%s-%s%s"% (self.add1.GetValue(),self.add.GetValue(),newline))
file.close()
#def AddBuch(self, event, buch):
#self.asked.SetValue("%s%s" % (self.asked.GetValue(),unicode(buch)))
def OnC(self, event):
#print event
self.lastfocus.SetValue("%s%s" % (self.lastfocus.GetValue(),u"ç"))
def OnS(self, event):
#print event
self.lastfocus.SetValue("%s%s" % (self.lastfocus.GetValue(),u"ş"))
def OnG(self, event):
self.lastfocus.SetValue("%s%s" % (self.lastfocus.GetValue(),u"ğ"))
def OnI(self, event):
self.lastfocus.SetValue("%s%s" % (self.lastfocus.GetValue(),u"ı"))
def OnClose(self, event):
print wx.Window.FindFocus()
self.Destroy()
class MyApp(wx.App):
def OnInit(self):
dlg = MyDialog(None, -1, 'TurkVoc')
dlg.Show(True)
dlg.Centre()
return True
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()