2014-04-25 95 views
1

的GUI中獲取變量,對不起我的英語 - 我仍在學習。我是來自德國的學生,我學習Python。首先,從程序

我有一個程序需要很多參數來運行,所以我通過wxGlade創建了一個gui。現在我想在我的應用程序中獲得這個參數。我看到了一些東西。他們使用GUI來編輯INI文件。應用程序從這些INI獲取參數。但這不是我想要的。我想用GUI來控制我的應用程序。 這是非常重要的,我可以保存我的價值觀在GUI(使用戶不應該再做一切)。

希望你明白我的意思。 這裏是我的GUI代碼(未準備好,但它是足夠做的第一個步驟)

這是我的GUI代碼:

#!/usr/bin/env python 
    # -*- coding: UTF-8 -*- 
    # 
    # generated by wxGlade 0.6.8 (standalone edition) on Thu Apr 24 12:36:34 2014 
    # 

    import wx 

    # begin wxGlade: dependencies 
    import gettext 
    # end wxGlade 

    # begin wxGlade: extracode 
    # end wxGlade 


    class MyFrame(wx.Frame): 
     def __init__(self, *args, **kwds): 
      # begin wxGlade: MyFrame.__init__ 
      kwds["style"] = wx.DEFAULT_FRAME_STYLE 
      wx.Frame.__init__(self, *args, **kwds) 

      # Menu Bar 
      self.frame_3_menubar = wx.MenuBar() 
      wxglade_tmp_menu = wx.Menu() 
      wxglade_tmp_menu.Append(wx.ID_ANY, _("Beenden"), "", wx.ITEM_NORMAL) 
      self.frame_3_menubar.Append(wxglade_tmp_menu, _("Datei")) 
      wxglade_tmp_menu = wx.Menu() 
      self.frame_3_menubar.Append(wxglade_tmp_menu, _("Bearbeiten")) 
      wxglade_tmp_menu = wx.Menu() 
      wxglade_tmp_menu.Append(wx.ID_ANY, _("Dokumenationen"), "", wx.ITEM_NORMAL) 
      self.frame_3_menubar.Append(wxglade_tmp_menu, _("Hilfe")) 
      self.SetMenuBar(self.frame_3_menubar) 
      # Menu Bar end 
      self.frame_3_statusbarr = self.CreateStatusBar(1, 0) 
      self.kartei = wx.Notebook(self, wx.ID_ANY, style=0) 
      self.pane_all_settings = wx.Panel(self.kartei, wx.ID_ANY) 
      self.label_5 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Laufzeiteinstellungen")) 
      self.label_6 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Abrechnungsjahr")) 
      self.abr_jahr = wx.SpinCtrl(self.pane_all_settings, wx.ID_ANY, "", min=2000, max=2099, style=wx.SP_ARROW_KEYS | wx.TE_AUTO_URL) 
      self.label_7 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Abrechnungmonat")) 
      self.abr_monat = wx.SpinCtrl(self.pane_all_settings, wx.ID_ANY, "", min=1, max=12) 
      self.label_8 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Payroll")) 
      self.payroll = wx.ComboBox(self.pane_all_settings, wx.ID_ANY, choices=[_("Loga"), _("Sage"), _("SAP"), _("KidiCap"), _("fidelis Personal")], style=wx.CB_DROPDOWN) 
      self.label_1 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Mandant")) 
      self.mandant = wx.SpinCtrl(self.pane_all_settings, wx.ID_ANY, "1", min=0, max=999, style=wx.SP_ARROW_KEYS | wx.TE_AUTO_URL) 
      self.zuschlag = wx.CheckBox(self.pane_all_settings, wx.ID_ANY, _(u"Zuschl\xe4ge")) 
      self.fehlzeit = wx.CheckBox(self.pane_all_settings, wx.ID_ANY, _("Fehlzeiten")) 
      self.urlaub = wx.CheckBox(self.pane_all_settings, wx.ID_ANY, _(u"Urlaubsanspr\xfcche")) 
      self.soll = wx.CheckBox(self.pane_all_settings, wx.ID_ANY, _("Sollstunden")) 
      self.label_8_copy_1 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("ImpVar")) 
      self.dir_impvar = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "") 
      self.label_8_copy_2 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("ImpUbr")) 
      self.dir_impubr = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "") 
      self.label_8_copy_1_copy = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Pfad zur ImpVar")) 
      self.dir_impvar_copy = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "") 
      self.label_8_copy_1_copy_copy = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Pfad zur ImpUbr")) 
      self.dir_impvar_copy_1 = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "") 
      self.label_8_copy_1_copy_copy_copy = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Ausgabeverzeichnis")) 
      self.dir_impvar_copy_2 = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "") 
      self.button_1 = wx.Button(self.pane_all_settings, wx.ID_ANY, _("Exportieren")) 
      self.button_1_copy = wx.Button(self.pane_all_settings, wx.ID_ANY, _("Abbrechen")) 
      self.pan_loga = wx.Panel(self.kartei, wx.ID_ANY) 
      self.label_5_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _("Exportieren nach Loga")) 
      self.label_6_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _("Loga-Mandant")) 
      self.loga_mandant = wx.SpinCtrl(self.pan_loga, wx.ID_ANY, "1", min=0, max=1000000, style=wx.SP_ARROW_KEYS | wx.TE_AUTO_URL) 
      self.label_7_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _("Loga-Abrechnungskreis")) 
      self.loga_al = wx.SpinCtrl(self.pan_loga, wx.ID_ANY, "", min=0, max=100) 
      self.label_8_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _("Empty")) 
      self.combo_box_1_copy = wx.ComboBox(self.pan_loga, wx.ID_ANY, choices=[], style=wx.CB_DROPDOWN) 
      self.label_1_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _(u"Personalnummer f\xfcllen")) 
      self.loga_fill_pnr = wx.SpinCtrl(self.pan_loga, wx.ID_ANY, "1", min=0, max=999, style=wx.SP_ARROW_KEYS | wx.TE_AUTO_URL) 
      self.konv_loa = wx.CheckBox(self.pan_loga, wx.ID_ANY, _("Konvertierungslohnart")) 
      self.konv_fehl = wx.CheckBox(self.pan_loga, wx.ID_ANY, _("Konvertierungsfehlzeiten")) 
      self.zeitraum_fehl = wx.CheckBox(self.pan_loga, wx.ID_ANY, _("Zeitraum Fehlzeit")) 
      self.vertragsnummer = wx.CheckBox(self.pan_loga, wx.ID_ANY, _(u"Vertragsnummer ber\xfccksichtigen")) 
      self.notebook_2_pane_3 = wx.Panel(self.kartei, wx.ID_ANY) 
      self.notebook_2_pane_4 = wx.Panel(self.kartei, wx.ID_ANY) 
      self.notebook_2_pane_5 = wx.Panel(self.kartei, wx.ID_ANY) 
      self.notebook_2_pane_6 = wx.Panel(self.kartei, wx.ID_ANY) 
      self.notebook_2_pane_7 = wx.Panel(self.kartei, wx.ID_ANY) 

      self.__set_properties() 
      self.__do_layout() 

      self.Bind(wx.EVT_MENU, self.stopExport, id=wx.ID_ANY) 
      self.Bind(wx.EVT_BUTTON, self.startExport, self.button_1) 
      self.Bind(wx.EVT_BUTTON, self.stopExport, self.button_1_copy) 
      # end wxGlade 

     def __set_properties(self): 
      # begin wxGlade: MyFrame.__set_properties 
      self.SetTitle(_("TDA Export Manager 0.12")) 
      self.frame_3_statusbarr.SetStatusWidths([-1]) 
      # statusbar fields 
      frame_3_statusbarr_fields = [_("(C) TDA-HR-Software Entwicklungs GmbH")] 
      for i in range(len(frame_3_statusbarr_fields)): 
       self.frame_3_statusbarr.SetStatusText(frame_3_statusbarr_fields[i], i) 
      self.payroll.SetSelection(-1) 
      # end wxGlade 

     def __do_layout(self): 
      # begin wxGlade: MyFrame.__do_layout 
      sizer_2 = wx.BoxSizer(wx.HORIZONTAL) 
      grid_sizer_2_copy = wx.FlexGridSizer(10, 4, 0, 0) 
      grid_sizer_2 = wx.FlexGridSizer(10, 4, 0, 0) 
      grid_sizer_2.Add(self.label_5, 0, wx.ALL, 10) 
      grid_sizer_2.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_6, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.abr_jahr, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_7, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.abr_monat, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_8, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.payroll, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_1, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.mandant, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.zuschlag, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.fehlzeit, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.urlaub, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.soll, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_8_copy_1, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.dir_impvar, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_8_copy_2, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.dir_impubr, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_8_copy_1_copy, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.dir_impvar_copy, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_8_copy_1_copy_copy, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.dir_impvar_copy_1, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.label_8_copy_1_copy_copy_copy, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.dir_impvar_copy_2, 0, wx.ALL, 10) 
      grid_sizer_2.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.button_1, 0, wx.ALL, 10) 
      grid_sizer_2.Add(self.button_1_copy, 0, wx.ALL, 10) 
      self.pane_all_settings.SetSizer(grid_sizer_2) 
      grid_sizer_2_copy.Add(self.label_5_copy, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.label_6_copy, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.loga_mandant, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.label_7_copy, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.loga_al, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.label_8_copy, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.combo_box_1_copy, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.label_1_copy, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.loga_fill_pnr, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.konv_loa, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.konv_fehl, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.zeitraum_fehl, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add(self.vertragsnummer, 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10) 
      self.pan_loga.SetSizer(grid_sizer_2_copy) 
      self.kartei.AddPage(self.pane_all_settings, _("Allgemeine Einstellungen")) 
      self.kartei.AddPage(self.pan_loga, _("Loga")) 
      self.kartei.AddPage(self.notebook_2_pane_3, _("Sage")) 
      self.kartei.AddPage(self.notebook_2_pane_4, _("SAP")) 
      self.kartei.AddPage(self.notebook_2_pane_5, _("KidiCap")) 
      self.kartei.AddPage(self.notebook_2_pane_6, _("fidelis Personal")) 
      self.kartei.AddPage(self.notebook_2_pane_7, _("Konvertierungsfehlzeiten")) 
      sizer_2.Add(self.kartei, 1, wx.EXPAND, 0) 
      self.SetSizer(sizer_2) 
      sizer_2.Fit(self) 
      self.Layout() 
      # end wxGlade 

     def startExport(self, event): # wxGlade: MyFrame.<event_handler> 
      abrjahr = self.abr_jahr.GetValue() 
      print abrjahr 

     def stopExport(self, event): # wxGlade: MyFrame.<event_handler> 
      self.Close() 


    # end of class MyFrame 



    if __name__ == "__main__": 
     gettext.install("app") # replace with the appropriate catalog name 
     app = wx.PySimpleApp(0) 
     wx.InitAllImageHandlers() 
     frame_3 = MyFrame(None, wx.ID_ANY, "") 
     app.SetTopWindow(frame_3) 
     frame_3.Show() 
     app.MainLoop() 
+0

'print(sys.argv)'這些是你需要的參數嗎? – Torxed

+0

No.May fffffffffffff – Kohonick

+0

啊沒關係,現在我明白你的意思了:P – Torxed

回答

0

有辦法獲取類對象並遍歷它們。
但我建議代碼的重組,並考慮爲圖形元素的容器,而不是像這樣:

self.frames = {} 

self.frames['_3_menubar'] = wx.MenuBar() 
self.frames['frame_3_statusbarr'] = self.CreateStatusBar(1, 0) 
self.frames['kartei'] = wx.Notebook(self, wx.ID_ANY, style=0) 
self.frames['pane_all_settings'] = wx.Panel(self.kartei, wx.ID_ANY) 
self.frames['label_5'] = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Laufzeiteinstellungen")) 
#... Etc etc, the rest of the objects 

# And do something like this not only for your own sake 
# But for getting rid huge amounts of code that can be compressed into stuff like this. 
for item in ('Beenden', 'Datei', 'Bearbeiten', 'Dokumentationen', 'Hilfe'): 
    self.frames['_3_menubar'].Append(wxglade_tmp_menu, _(item)) 

有了這個,你可以簡單地做:

def getAllValues(self): 
    for frame in self.frames: 
     value = self.getValue(self.frames[frame]) 
     yield (frame, value) 

或類似的東西,不知道你想要做的值,但你可以這樣做:

def saveAllValues(self): 
    with open('settings.ini', 'w') as fh: 
     for frame, value in self.getAllValues(): 
      fh.write(frame + ':' + str(value) + '\n') 
+0

好的 - 謝謝。 沒有大問題: 我下次如何保存我的數值? – Kohonick

+0

@ user3455995'設置。ini'將包含所有的值,只要用open('settings.ini','r')作爲fh:'並且爲'fh中的line'做'並且你再次得到每個單獨的設置:) – Torxed

0

我建議你做到以下幾點:

  • 使用字典來接收所有初始值。讓字典中的名稱映射到變量的名稱。
  • 允許其他模塊使用Observer Pattern訂閱此GUI。
  • 當發生任何變化或按下按鈕時,請創建一個包含更改的字典並通知所有訂戶該更改。

這使GUI與程序的其餘部分完全分離。