Related
I am trying to plot 1D txt files in a part of the window created using wxpython. For this purpose, a directory selection tool was included which lists all txt files. Now, I would like to select a txt file and plot it in a panel on the right side.
Further, I am thinking to implement a button that does some operations on the data and replot again.
What I have tried is this :
import os
import wx
import numpy as np
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
class mainFrame (wx.Frame):
def __init__(self):
super().__init__(None, id=wx.ID_ANY, title=u" test ",
size=wx.Size(854, 698),
style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetSizeHints(wx.Size(600, -1), wx.DefaultSize)
sizer = wx.BoxSizer(wx.VERTICAL)
self.panel = MainPanel(self, style=wx.TAB_TRAVERSAL)
self.Layout()
sizer.Add(self.panel, 1, wx.EXPAND, 0)
self.SetSizer(sizer)
self.Layout()
self.Centre()
# Connect Events
self.panel.dirPicker.Bind(wx.EVT_DIRPICKER_CHANGED, self.dirPickerOnDirChanged)
self.panel.listBox.Bind(wx.EVT_LISTBOX, self.listBoxOnListBox)
# ------------ Add widget program settings
# ------------ Call Populates
self.Show()
# Virtual event handlers, override them in your derived class
def dirPickerOnDirChanged(self, event):
self.FilePath = event.GetPath()
self.populateFileList()
def populateFileList(self):
self.panel.listBox.Clear()
allFiles = os.listdir(self.FilePath)
for file in allFiles:
if file.endswith('.txt'):
self.panel.listBox.Append(file)
def listBoxOnListBox(self, event):
try:
selected_file = event.GetString()
file_address = os.path.join(self.FilePath, selected_file)
# load file
data = np.loadtxt(file_address)
# select the first column
if isinstance(data, np.ndarray):
print("\tdata is np.array")
dim = data.ndim
if dim == 2:
input1D = data[:, 0]
else:
input1D = data
print(input1D.shape)
# plot here
else:
print("\tdata is not np.array")
except: # Do not use bare except
print("Some error.")
class MainPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.FONT_11 = wx.Font(11, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL, False, "Consolas")
self.FONT_12 = wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL, False, wx.EmptyString)
self.FONT_13 = wx.Font(13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL, False, wx.EmptyString)
self.FONT_14 = wx.Font(14, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_BOLD, False, "Consolas")
self.FONT_16 = wx.Font(16, wx.FONTFAMILY_SCRIPT, wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_BOLD, False, wx.EmptyString)
sizer = wx.BoxSizer(wx.VERTICAL)
quick_display = self._quick_display()
directory_sizer = self._directory_sizer()
list_box_sizer = self._list_box_sizer()
self.text_details = self._detail_input()
details_sizer = self._details_sizer()
status_sizer = self._status_sizer()
message_sizer = wx.BoxSizer(wx.VERTICAL)
message_sizer.Add(details_sizer, 1, wx.EXPAND, 5)
message_sizer.Add(status_sizer, 1, wx.EXPAND, 5)
sizer.Add(quick_display, 0, wx.EXPAND, 0)
sizer.Add(directory_sizer, 0, wx.EXPAND, 0)
sizer.Add(list_box_sizer, 1, wx.EXPAND, 0)
sizer.Add(message_sizer, 1, wx.EXPAND, 5)
self.SetSizer(sizer)
def _quick_display(self):
quick_display = wx.StaticText(self, label=u"quick display")
quick_display.Wrap(-1)
quick_display.SetFont(self.FONT_16)
return quick_display
def _directory_sizer(self):
sbSizerDir = wx.StaticBoxSizer(wx.StaticBox(self, label=u" working directory"))
self.dirPicker = wx.DirPickerCtrl(sbSizerDir.GetStaticBox(), message=u"Select a folder")
sbSizerDir.Add(self.dirPicker, 0, wx.ALL | wx.EXPAND, 5)
return sbSizerDir
def _list_box(self):
listBoxChoices = []
self.listBox = wx.ListBox(self, size=wx.Size(300, -1), choices=listBoxChoices)
self.listBox.SetMinSize(wx.Size(250, -1))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.listBox, 1, wx.ALL, 10)
return sizer
def _plot_sizer(self):
self.panelPlot = PlotPanel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.panelPlot, 1, wx.EXPAND | wx.ALL, 5)
return sizer
def _list_box_sizer(self):
file_list_sizer = self._list_box()
bSizer_plot = self._plot_sizer()
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(file_list_sizer, 1, wx.EXPAND, 0)
sizer.Add(bSizer_plot, 1, wx.EXPAND, 5)
bSizerSplitHor = wx.BoxSizer(wx.HORIZONTAL)
bSizerSplitHor.Add(sizer, 1, wx.EXPAND, 2)
bSizerSplit = wx.BoxSizer(wx.VERTICAL)
bSizerSplit.Add(bSizerSplitHor, 1, wx.EXPAND, 0)
return bSizerSplit
def _detail_label(self):
detail_label = wx.StaticText(self, label="Details")
detail_label.Wrap(-1)
detail_label.SetFont(self.FONT_14)
return detail_label
def _detail_input(self):
text_details = wx.TextCtrl(self, size=wx.Size(250, -1))
text_details.SetFont(self.FONT_11)
return text_details
def _button_sizer(self):
self.button = wx.Button(self, label=u"do some operation")
self.button.SetFont(self.FONT_13)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.button, 0, wx.ALL, 5)
return sizer
def _details_sizer(self):
detail_label = self._detail_label()
button_sizer = self._button_sizer()
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(detail_label, 0, wx.ALL, 5)
sizer.Add(self.text_details, 1, wx.EXPAND, 5)
sizer.Add(button_sizer, 1, wx.EXPAND, 5)
return sizer
def _status_sizer(self):
self.staticline3 = wx.StaticLine(self)
self.status_label = wx.StaticText(self, label=u"Status bar")
self.status_label.Wrap(-1)
self.status_label.SetFont(self.FONT_12)
self.staticline4 = wx.StaticLine(self)
self.textCtrl_status = wx.TextCtrl(self)
self.textCtrl_status.SetFont(self.FONT_11)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.staticline3, 0, wx.EXPAND | wx.ALL, 5)
sizer.Add(self.status_label, 0, wx.ALL, 5)
sizer.Add(self.staticline4, 0, wx.EXPAND | wx.ALL, 5)
sizer.Add(self.textCtrl_status, 0, wx.ALL | wx.EXPAND, 5)
status_sizer = wx.BoxSizer(wx.VERTICAL)
status_sizer.Add(sizer, 1, wx.EXPAND, 5)
return status_sizer
class PlotPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.SetMinSize(wx.Size(100, -1))
if __name__ == "__main__":
app = wx.App(False)
frame = mainFrame()
app.MainLoop()
This creates a window as follows :
The right portion is assigned as a panel, and I not sure how to place the matplotlib plot in it. Thank you.
There are several good questions on SE probing this topic, such as Q1 and Q2; however, most are limited to the plot being shown on the main window.
wxGlade includes some wxPython / matplotlib examples. Use these as starting point.
https://github.com/wxGlade/wxGlade/tree/master/examples
I have this class which deals with listBox:
I tried to make a delete button for listBox items but it didn't work. The "deleteItem" function does now work.Without the marked parts it works good, but there is no delete option. Help someone?
class SettingProcess(wx.Frame):
itemsArr= []
# ----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Black list Proceses",size=(500, 600))
self.Centre()
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY,size=(500, 600))
self.index = 0
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
lbl_process = wx.StaticText(panel, -1, "Process name")
lbl_process.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
self.txt_processname = wx.TextCtrl(panel, size=(140, -1))
self.txt_processname.SetValue("chrome.exe")
sizer.AddMany([lbl_process, self.txt_processname])
#------------- Files ListBox -------------------------------------
global ProcessListMain
self.files_listBox = wx.ListBox(panel, -1, (12, 130), (200, 240), ProcessListMain,
wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_ALWAYS_SB | wx.LB_SORT)
self.files_listBox.SetBackgroundColour(wx.Colour(255, 255, 128))
#------------- Set event handlers ---------------------------------
m_start = wx.Button(panel, wx.ID_CLOSE, "Add")
m_start.Bind(wx.EVT_BUTTON, self.OnAddButton)
sizer.Add(m_start, 0, wx.ALL, 10)
m_close = wx.Button(panel, wx.ID_CLOSE, "Close")
m_close.Bind(wx.EVT_BUTTON, self.onExit)
sizer.Add(m_close, 0, wx.ALL, 10)
**m_delete = wx.Button(panel, wx.ID_CLOSE, "Delete")
m_delete.Bind(wx.EVT_BUTTON, self.deleteItem)
sizer.Add(m_delete, 0, wx.ALL, 10)**
panel.SetSizer(sizer)
# ----------------------------------------------------------------------
def OnAddButton(self,str):
global ProcessListMain
#print "value is "+ str(self.txt_processname.GetValue())
ProcessListMain.append(self.txt_processname.GetValue())
self.files_listBox.Set(ProcessListMain)
def onExit(self, event):
self.Destroy()
***def deleteItem(self,event):
numOfItems=self.files_listBox.GetCount()
for i in range(numOfItems):
self.itemsArr[i]=self.files_listBox.GetString()
selectedItems=self.files_listBox.GetStringSelection()
self.files_listBox.clear()
for i in numOfItems :
if self.itemsArr[i]!=selectedItems:
self.files_listBox.append( self.itemsArr[i])***
Here is the version, courtesy of Robin Dunn's comment:
Note that I have also bound the textctrl so that just filling in a process name and pressing Enter, also acts like the Add button.
import wx
class SettingProcess(wx.Frame):
# ----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Black list Processes",size=(500, 600))
self.Centre()
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY,size=(500, 600))
self.index = 0
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
lbl_process = wx.StaticText(panel, -1, "Process name")
lbl_process.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
self.txt_processname = wx.TextCtrl(panel, size=(140, -1), style=wx.TE_PROCESS_ENTER)
self.txt_processname.SetValue("chrome.exe")
self.txt_processname.Bind(wx.EVT_TEXT_ENTER, self.OnAddButton)
sizer.AddMany([lbl_process, self.txt_processname])
#------------- Files ListBox -------------------------------------
ProcessListMain = ['Process A','Process B','Process C','Process D','Process E']
self.files_listBox = wx.ListBox(panel, -1, (12, 130), (200, 240), ProcessListMain,
wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_ALWAYS_SB | wx.LB_SORT)
self.files_listBox.SetBackgroundColour(wx.Colour(255, 255, 128))
#------------- Set event handlers ---------------------------------
m_start = wx.Button(panel, -1, "Add")
m_start.Bind(wx.EVT_BUTTON, self.OnAddButton)
sizer.Add(m_start, 0, wx.ALL, 10)
m_close = wx.Button(panel, -1, "Close")
m_close.Bind(wx.EVT_BUTTON, self.onExit)
sizer.Add(m_close, 0, wx.ALL, 10)
m_delete = wx.Button(panel, -1, "Delete")
m_delete.Bind(wx.EVT_BUTTON, self.deleteItem)
sizer.Add(m_delete, 0, wx.ALL, 10)
panel.SetSizer(sizer)
# ----------------------------------------------------------------------
def OnAddButton(self,event):
Addproc = self.txt_processname.GetValue()
if Addproc:
self.files_listBox.Append(Addproc)
def onExit(self, event):
self.Destroy()
def deleteItem(self,event):
deleted_item = self.files_listBox.GetSelection()
self.files_listBox.Delete(deleted_item)
if __name__=='__main__':
app = wx.App()
frame = SettingProcess()
frame.Show()
app.MainLoop()
It's a bit more complicated than having duplicate Id's for your buttons, as I suggested earlier.
Try the following: (I have stuck with your global ProcessListMain but had to mock it up, as it isn't declared in your questions code)
import wx
class SettingProcess(wx.Frame):
itemsArr= []
# ----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Black list Processes",size=(500, 600))
self.Centre()
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY,size=(500, 600))
self.index = 0
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
lbl_process = wx.StaticText(panel, -1, "Process name")
lbl_process.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
self.txt_processname = wx.TextCtrl(panel, size=(140, -1))
self.txt_processname.SetValue("chrome.exe")
sizer.AddMany([lbl_process, self.txt_processname])
#------------- Files ListBox -------------------------------------
global ProcessListMain
ProcessListMain = ['123','456','789','1123','2123']
self.files_listBox = wx.ListBox(panel, -1, (12, 130), (200, 240), ProcessListMain,
wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_ALWAYS_SB | wx.LB_SORT)
self.files_listBox.SetBackgroundColour(wx.Colour(255, 255, 128))
#------------- Set event handlers ---------------------------------
m_start = wx.Button(panel, -1, "Add")
m_start.Bind(wx.EVT_BUTTON, self.OnAddButton)
sizer.Add(m_start, 0, wx.ALL, 10)
m_close = wx.Button(panel, -1, "Close")
m_close.Bind(wx.EVT_BUTTON, self.onExit)
sizer.Add(m_close, 0, wx.ALL, 10)
m_delete = wx.Button(panel, -1, "Delete")
m_delete.Bind(wx.EVT_BUTTON, self.deleteItem)
sizer.Add(m_delete, 0, wx.ALL, 10)
panel.SetSizer(sizer)
# ----------------------------------------------------------------------
def OnAddButton(self,event):
global ProcessListMain
Addproc = self.txt_processname.GetValue()
ProcessListMain.append(Addproc)
self.files_listBox.Set(ProcessListMain)
def onExit(self, event):
self.Destroy()
def deleteItem(self,event):
global ProcessListMain
deleted_item = self.files_listBox.GetStringSelection()
numOfItems=self.files_listBox.GetCount()
itemsArr = []
for i in range(numOfItems):
x = self.files_listBox.GetString(i)
if x != deleted_item:
itemsArr.append(x)
ProcessListMain = itemsArr
self.files_listBox.Clear()
self.files_listBox.Set(ProcessListMain)
self.files_listBox.Update()
if __name__=='__main__':
app = wx.App()
frame = SettingProcess()
frame.Show()
app.MainLoop()
first, sorry for my english - im still learning. I´m a student from Germany and i learn Python.
I have a program which needs a lot of paramters for running so i build a gui by wxGlade. Now i want to get this paramters in my application. I saw some things. They used the GUI for editing a INI- File. And the application gets the paramters from these INI. But this is not what i want. I want to controll my application with the GUI.
And it is very Important that i can save my Values in the GUI (so that the User should not do everything again).
Hope you understand what i mean.
Here is my Code for the Gui (not ready but it is enough for doing the first steps)
Here is my GUI Code:
#!/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()
There are ways to get the class objects and iterate over them.
But i'd suggest a restructure of the code and consider a container for your graphical elements instead, like this:
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))
With this you can simply do:
def getAllValues(self):
for frame in self.frames:
value = self.getValue(self.frames[frame])
yield (frame, value)
Or something similar, not sure what you want to do with the values but you could do:
def saveAllValues(self):
with open('settings.ini', 'w') as fh:
for frame, value in self.getAllValues():
fh.write(frame + ':' + str(value) + '\n')
I would suggest you do the following:
Use a dictionary to receive all the initial values. Let the names in the dictionary map to the names of your variables.
Allow other modules to subscribe to this GUI using the Observer Pattern.
When anything changes, or when the buttons are pressed, create a dictionary with the changes and notify all subscribers of the change.
This keeps the GUI cleanly separated from the rest of the program.
For some reason my sizer.Clear() does not seem to work properly.
As far as I have understood from the docs it should work.
Am I doing something wrong that causes this behavior?
import wx
import os
import sys
import time
import string
import urllib2
class MainWindow(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.MINIMIZE_BOX | wx.CLOSE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(sizer)
self.sizer = sizer
self.panel = panel
self.ShowLoginBox()
def ShowLoginBox(self):
sizer = wx.FlexGridSizer(rows=4, cols=2, hgap=15, vgap=10)
sizer.AddGrowableCol(1, 1)
login_url = wx.TextCtrl(self.panel, 1, size=(150, -1))
label_url = wx.StaticText(self.panel, -1, "URL:")
sizer.Add(label_url, 0, wx.LEFT | wx.TOP| wx.RIGHT, 30)
sizer.Add(login_url, 1, wx.EXPAND | wx.TOP | wx.RIGHT, 30)
login_username = wx.TextCtrl(self.panel, 1, size=(150, -1))
label_username = wx.StaticText(self.panel, -1, "Username:")
sizer.Add(label_username, 0, wx.LEFT | wx.RIGHT, 30)
sizer.Add(login_username, 1, wx.EXPAND | wx.RIGHT, 30)
login_password = wx.TextCtrl(self.panel, 1, size=(150, -1), style=wx.TE_PASSWORD)
label_password = wx.StaticText(self.panel, -1, "Password:")
sizer.Add(label_password, 0, wx.LEFT | wx.RIGHT, 30)
sizer.Add(login_password, 1, wx.EXPAND | wx.RIGHT, 30)
btn_process = wx.Button(self.panel, -1, "&Login")
self.panel.Bind(wx.EVT_BUTTON, self.OnSubmit, btn_process)
sizer.Add(btn_process, 0, wx.LEFT, 30)
login_url.SetValue("http://example.com")
login_username.SetValue("admin")
login_password.SetValue("pass")
self.login_url = login_url
self.login_username = login_username
self.login_password = login_password
self.sizer.Clear()
self.sizer.Add(sizer, 1, wx.EXPAND)
self.SetSizeWH(330, 250)
self.Center()
def OnSubmit(self, event):
user_url = self.login_url.GetValue()
user_name = self.login_username.GetValue()
user_pass = self.login_password.GetValue()
# login info is used to get a URL
print user_url, user_name, user_pass
# if 200 OK
self.ShowNew()
def ShowNew(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
content = wx.ListCtrl(self.panel, -1, size=(780, 400), style=wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_HRULES | wx.LC_VRULES)
content.InsertColumn(0, 'URL', width=745)
sizer.Add(content, 1, wx.EXPAND)
# populate with data from URL
self.SetSizeWH(800, 520)
self.Center()
self.sizer.Clear()
self.sizer.Add(sizer)
if __name__ == '__main__':
home_path = os.path.dirname(os.path.realpath(__file__))
app = wx.PySimpleApp()
frame = MainWindow(None, title="Test", size=(800, 520))
frame.SetBackgroundColour("#ffffff")
frame.Show()
app.MainLoop()
Use sizer.Clear(True) instead of sizer.Clear() and it should work.
The explanation
If the parameter sent to Clear() method is True, "then child windows will also be deleted" (http://docs.wxwidgets.org/2.8/wx_wxsizer.html#wxsizerclear).
You want to clear the children of wxSizer: wxStaticText, wxTextCtrl, wxButton, which are all derived from wxControl and wxWindow. That's why the sizer must know that you want to remove also the child windows.
See also: http://docs.wxwidgets.org/2.8/wx_wxstatictext.html, http://docs.wxwidgets.org/2.8/wx_wxtextctrl.html and http://docs.wxwidgets.org/2.8/wx_wxbutton.html .
Cheers!
I am trying to nest an sizer with horizontal layout inside the vertical layout sizer of a notebookPage panel.
I took some inspiration from wxPython: Resizing a notebook within a notebook but cannot make it work properly.
Instead of getting this layout
Tonnage limit (in tons): [3000]
[x] Enable filter on matchings categories
Everything is mangled on a single line in the panel
I am running this on Win7, python 2.6.6, wx 2.8.12.1
Here is the relevant code:
import sys
import os
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,title):
self.ID_LOAD=101
self.ID_SAVE=102
# based on a frame, so set up the frame
wx.Frame.__init__(self,parent,wx.ID_ANY, title, size=(640,480))
self.CreateStatusBar()
self.tab = wx.Notebook(self, -1, style=(wx.NB_TOP))
self.globPanel = wx.NotebookPage(self.tab, -1)
self.orderPanel = wx.NotebookPage(self.tab, -1)
self.slabsPanel = wx.NotebookPage(self.tab, -1)
self.tab.AddPage(self.globPanel, "Global filter")
self.tab.AddPage(self.orderPanel, "Orders filter")
self.tab.AddPage(self.slabsPanel, "Slabs filter")
self.toolbar = wx.ToolBar(self, wx.ID_ANY, style=wx.TB_HORIZONTAL)
self.toolbar.SetToolBitmapSize((20,20))
load_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (20,20))
self.toolbar.AddLabelTool(self.ID_LOAD, "Load", load_ico, bmpDisabled=wx.NullBitmap, shortHelp='Load', longHelp="Loads a text file to the filter editor")
save_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR, (20,20))
self.toolbar.AddLabelTool(self.ID_SAVE, "Save", save_ico, bmpDisabled=wx.NullBitmap, shortHelp='Save', longHelp="Saves the contents of the filter editor")
self.toolbar.AddSeparator()
self.tonnageStatic = wx.StaticText(self.globPanel, wx.ID_ANY, "Tonnage limit (in tons): ")
self.tonnageEdit = wx.TextCtrl(self.globPanel, wx.ID_ANY, '30000')
self.tonnageSizer = wx.BoxSizer(wx.HORIZONTAL)
self.tonnageSizer.Add(self.tonnageStatic, flag=wx.EXPAND | wx.RIGHT | wx.LEFT | wx.TOP | wx.BOTTOM, border=10)
self.tonnageSizer.Add(self.tonnageEdit, flag=wx.EXPAND | wx.RIGHT | wx.LEFT | wx.TOP | wx.BOTTOM, border=10)
self.filterCheck = wx.CheckBox(self.globPanel, wx.ID_ANY, "Enable filter on matchings categories")
self.globSizer = wx.BoxSizer(wx.VERTICAL)
self.globSizer.Add(self.tonnageSizer)
self.globSizer.Add(wx.StaticLine(self), 0, wx.EXPAND)
self.globSizer.Add(self.filterCheck, 0, wx.EXPAND)
self.globPanel.SetSizer(self.globSizer)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.toolbar,0,wx.EXPAND)
self.sizer.Add(wx.StaticLine(self), 0, wx.EXPAND)
self.sizer.Add(self.tab, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.Layout()
self.toolbar.Realize()
# Show it !!!
self.Show(1)
def main():
app = wx.PySimpleApp()
view = MainWindow(None, "Input filter")
app.MainLoop()
if __name__ == "__main__":
main()
It is slighlty better like this, but I don't get the static line and only the first character of the checkox label is displayed
self.tonnageStatic = wx.StaticText(self.globPanel, wx.ID_ANY, "Tonnage limit (in tons): ")
self.tonnageEdit = wx.TextCtrl(self.globPanel, wx.ID_ANY, '30000')
self.tonnageSizer = wx.BoxSizer(wx.HORIZONTAL)
self.tonnageSizer.Add(self.tonnageStatic, 0, flag=wx.EXPAND|wx.ALL)
self.tonnageSizer.Add(self.tonnageEdit, 0, flag=wx.EXPAND|wx.ALL)
self.filterCheck = wx.CheckBox(self.globPanel, wx.ID_ANY, "Enable filter on matchings categories")
self.globSizer = wx.BoxSizer(wx.VERTICAL)
self.globSizer.Add(self.tonnageSizer)
self.globSizer.Add(wx.StaticLine(self.globPanel), 0, wx.EXPAND)
self.globSizer.Add(self.filterCheck, 0, wx.EXPAND)
self.globPanel.SetSizer(self.globSizer)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.toolbar,0,wx.EXPAND)
self.sizer.Add(wx.StaticLine(self), 0, wx.EXPAND)
self.sizer.Add(self.tab, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.globPanel.Layout()
self.Layout()
self.toolbar.Realize()
# Show it !!!
self.Show(1)
I just ran it on windows and if you call .Layout() on the globPanel after the self.Layout() line it should kick the sizers into shape :)
Also you can use wx.ALL which is the same as right, left, top and bottom
EDIT: I was curious why the weird behaviour and looked into a bit more. I looked at places I've used notebooks and I always use panels for the pages. If you change the NotebookPages to Panels you can do away with the Layout calls and it works as expected.