Why does this StaticLine appear behind the sizer? - python

I'm trying to draw a static line between the "Label"+TextCtrl and the radio buttons.
It keeps appearing only when the window is dragged, and then it appears behind everything and I can't figure out why.
The goal is to have the static line draw horizontally between the upper and the lower section.
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((237, 237))
self.__initUI()
self.__do_layout()
def __initUI(self):
panel = wx.Panel(self)
self.SetSize((350, 150))
self.qtyField = wx.TextCtrl(panel, wx.ID_ANY, "", style=wx.TE_CENTER)
self.qtyField.SetFocus()
self.longRb = wx.RadioButton(panel, wx.ID_ANY, "This", style=wx.RB_GROUP)
self.shortRb = wx.RadioButton(panel, wx.ID_ANY, "That")
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
vertSizer = wx.BoxSizer(wx.VERTICAL)
horSizer1 = wx.GridSizer(1, 2, 0, 0)
rbSizer = wx.GridSizer(1, 2, 0, 36)
qtyLabel = wx.StaticText(self, wx.ID_ANY, "Label")
horSizer1.Add(qtyLabel, 0, wx.ALIGN_CENTER, 0)
horSizer1.Add(self.qtyField, 0, wx.ALIGN_CENTER_VERTICAL, 0)
vertSizer.Add(horSizer1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)
static_line_1 = wx.StaticLine(self, wx.ID_ANY)
vertSizer.Add(static_line_1, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 6)
rbSizer.Add(self.longRb, 0, wx.ALIGN_CENTER, 0)
rbSizer.Add(self.shortRb, 0, wx.ALIGN_CENTER, 0)
vertSizer.Add(rbSizer, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)
self.SetSizer(vertSizer)
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(self.frame)
self.frame.Show()
return True
# end of class MyApp
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()

Your are assigning some widgets to the Frame (self) and others to the panel, so they present themselves where instructed to do so.
This is what you are after:
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((237, 237))
self.__initUI()
self.__do_layout()
def __initUI(self):
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour("green")
self.SetSize((350, 150))
self.qtyField = wx.TextCtrl(self.panel, wx.ID_ANY, "", style=wx.TE_CENTER)
self.qtyField.SetFocus()
self.longRb = wx.RadioButton(self.panel, wx.ID_ANY, "This", style=wx.RB_GROUP)
self.shortRb = wx.RadioButton(self.panel, wx.ID_ANY, "That")
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
vertSizer = wx.BoxSizer(wx.VERTICAL)
horSizer1 = wx.GridSizer(1, 2, 0, 0)
rbSizer = wx.GridSizer(1, 2, 0, 36)
qtyLabel = wx.StaticText(self.panel, wx.ID_ANY, "Label")
horSizer1.Add(qtyLabel, 0, wx.ALIGN_CENTER, 0)
horSizer1.Add(self.qtyField, 0, wx.ALIGN_CENTER_VERTICAL, 0)
vertSizer.Add(horSizer1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)
static_line_1 = wx.StaticLine(self.panel, wx.ID_ANY)
vertSizer.Add(static_line_1, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 6)
rbSizer.Add(self.longRb, 0, wx.ALIGN_CENTER, 0)
rbSizer.Add(self.shortRb, 0, wx.ALIGN_CENTER, 0)
vertSizer.Add(rbSizer, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)
self.SetSizer(vertSizer)
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(self.frame)
self.frame.Show()
return True
# end of class MyApp
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()

Related

Show matplotlib plot in a wxpython panel and update on button push

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

Wxpython How to get the ComboBox's value of Panel1 from Panel2

Now I have two Panel Panelone and Paneltwo and use notebook in a Frame
and When I click the button, I want to return the value of Panelone to PanelTwo
like
class PanelTwo(wx.panel):
def __init__(self,parent):
super(PanelTwo,self).__init__(parent)
self.choice1 = wx.ComboBox(self,value=**the Panelone Value**,choices=,style=wx.CB_SORT, pos=(100, 5)) or
self.choice1.SetValue(the Panelone Value)
class Panelone(wx.panel):
def __init__(self,parent):
choicelist = ['1','2','3','5','6']
super(Panelone,self).__init__(parent)
self.choice = wx.ComboBox(self,value="1",choices=choicelist,style=wx.CB_SORT, pos=(100, 5))
self.btn = wx.Button(self, label="Summit",pos=(250, 10), size=(80, 50))
self.Bind(wx.EVT_BUTTON, self.BtnCheck, self.btn)
def BtnCheck(self,event):
**When I click the button, I want to return the value of Panelone to PanelTwo**
class Game(wx.Frame):
def __init__(self, parent, title):
super(Game, self).__init__(parent, title=title, size=(900, 700))
self.InitUI()
def InitUI(self):
nb = wx.Notebook(self)
nb.AddPage(PanelOne(nb), "PanelOne")
nb.AddPage(PanelTwo(nb), "PanelTwo")
self.Centre()
self.Show(True)
First of all, if you are learning, I recommend that you use WxGlade to build your graphical interfaces. Your code is pure spaghetti and is full of syntactic errors :(.
In the case of your example, it is very simple since all the elements belong to the same file and are in the same class.
For example:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import wx
# THE MAIN FRAME:
class MainFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((734, 501))
# The main notebook:
self.main_notebook = wx.Notebook(self, wx.ID_ANY)
# Notebook's panels:
self.panel1 = wx.Panel(self.main_notebook, wx.ID_ANY)
self.panel2 = wx.Panel(self.main_notebook, wx.ID_ANY)
# Content of panel1:
self.choiceFruits = wx.Choice(self.panel1, wx.ID_ANY, choices=[])
self.btn_send = wx.Button(self.panel1, wx.ID_ANY, "Send Value to Panel2")
#Content of panel2:
self.txt_result = wx.TextCtrl(self.panel2, wx.ID_ANY, "")
#Binding events:
# event, custom event handler, gui element
self.Bind(wx.EVT_BUTTON, self.OnBtnSendClick, self.btn_send)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle("frame")
choices = ['Apple', 'Banana', 'Peach', 'Strawberry']
self.choiceFruits.SetItems(choices)
self.choiceFruits.SetSelection(0)
def __do_layout(self):
# begin wxGlade: MainFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
grid_sizer_2 = wx.FlexGridSizer(2, 1, 0, 0)
grid_sizer_1 = wx.FlexGridSizer(2, 2, 0, 10)
label_1 = wx.StaticText(self.panel1, wx.ID_ANY, "Choose a fruit:")
grid_sizer_1.Add(label_1, 0, wx.ALL, 10)
grid_sizer_1.Add((0, 0), 0, 0, 0)
grid_sizer_1.Add(self.choiceFruits, 0, wx.BOTTOM | wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
grid_sizer_1.Add(self.btn_send, 0, wx.BOTTOM | wx.EXPAND | wx.RIGHT, 10)
self.panel1.SetSizer(grid_sizer_1)
grid_sizer_1.AddGrowableCol(0)
label_2 = wx.StaticText(self.panel2, wx.ID_ANY, "You have selected:")
grid_sizer_2.Add(label_2, 0, wx.ALL, 10)
grid_sizer_2.Add(self.txt_result, 0, wx.ALL | wx.EXPAND, 10)
self.panel2.SetSizer(grid_sizer_2)
grid_sizer_2.AddGrowableCol(0)
self.main_notebook.AddPage(self.panel1, "Panel 1")
self.main_notebook.AddPage(self.panel2, "Panel 2")
sizer_1.Add(self.main_notebook, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
self.Layout()
# end wxGlade
# Custom event handler:
def OnBtnSendClick(self, event):
selectedFruit = self.choiceFruits.GetString(self.choiceFruits.GetSelection())
self.txt_result.SetValue(selectedFruit)
wx.MessageBox("You have selected \"%s\"" % selectedFruit)
# The Main Class:
class MyApp(wx.App):
def OnInit(self):
self.main_frame = MainFrame(None, wx.ID_ANY, "")
self.SetTopWindow(self.main_frame)
self.main_frame.Show()
return True
# Main APP Method.
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
But this is not common. Usually each panel will be in a separate file within its own class. In that case, you must pass a reference from the main frame to each panel and then we use this reference to access the elements on the main frame (for example, another panel).
MAINFRAME
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import wx
from panel1 import Panel1
from panel2 import Panel2
# THE MAIN FRAME:
class MainFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((734, 501))
# The main notebook:
self.main_notebook = wx.Notebook(self, wx.ID_ANY)
# Notebook's panels:
self.panel1 = Panel1(self.main_notebook, wx.ID_ANY)
self.panel2 = Panel2(self.main_notebook, wx.ID_ANY)
# Pass reference of the main frame to each panel:
self.panel1.SetParent(self)
self.panel2.SetParent(self)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle("frame")
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
grid_sizer_2 = wx.FlexGridSizer(2, 1, 0, 0)
self.main_notebook.AddPage(self.panel1, "Panel 1")
self.main_notebook.AddPage(self.panel2, "Panel 2")
sizer_1.Add(self.main_notebook, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
self.Layout()
# The Main Class:
class MyApp(wx.App):
def OnInit(self):
self.main_frame = MainFrame(None, wx.ID_ANY, "")
self.SetTopWindow(self.main_frame)
self.main_frame.Show()
return True
# Main APP Method.
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
PANEL 1
# -*- coding: UTF-8 -*-
import wx
class Panel1(wx.Panel):
def __init__(self, *args, **kwds):
# begin wxGlade: Panel1.__init__
kwds["style"] = kwds.get("style", 0) | wx.TAB_TRAVERSAL
wx.Panel.__init__(self, *args, **kwds)
# Content of panel1:
self.choiceFruits = wx.Choice(self, wx.ID_ANY, choices=[])
self.btn_send = wx.Button(self, wx.ID_ANY, "Send Value to Panel2")
self._parent = None
#Binding events:
# event, custom event handler, gui element
self.Bind(wx.EVT_BUTTON, self.OnBtnSendClick, self.btn_send)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
choices = ['Apple', 'Banana', 'Peach', 'Strawberry']
self.choiceFruits.SetItems(choices)
self.choiceFruits.SetSelection(0)
def __do_layout(self):
grid_sizer_2 = wx.FlexGridSizer(2, 2, 0, 0)
label_2 = wx.StaticText(self, wx.ID_ANY, "Choose a fruit:")
grid_sizer_2.Add(label_2, 0, wx.LEFT | wx.RIGHT | wx.TOP, 10)
grid_sizer_2.Add((0, 0), 0, 0, 0)
grid_sizer_2.Add(self.choiceFruits, 0, wx.ALL | wx.EXPAND, 10)
grid_sizer_2.Add(self.btn_send, 0, wx.BOTTOM | wx.RIGHT | wx.TOP, 10)
self.SetSizer(grid_sizer_2)
grid_sizer_2.Fit(self)
grid_sizer_2.AddGrowableCol(0)
self.Layout()
# end wxGlade
def SetParent(self, parent):
self._parent = parent
# Custom event handler:
def OnBtnSendClick(self, event):
selectedFruit = self.choiceFruits.GetString(self.choiceFruits.GetSelection())
# here is the trick !!!
self._parent.panel2.txt_result.SetValue(selectedFruit)
wx.MessageBox("You have selected \"%s\"" % selectedFruit)
PANEL 2
# -*- coding: UTF-8 -*-
import wx
class Panel2(wx.Panel):
def __init__(self, *args, **kwds):
kwds["style"] = kwds.get("style", 0) | wx.TAB_TRAVERSAL
wx.Panel.__init__(self, *args, **kwds)
#Content of panel2:
self.txt_result = wx.TextCtrl(self, wx.ID_ANY, "")
self.__set_properties()
self.__do_layout()
def __set_properties(self):
pass
def __do_layout(self):
grid_sizer_2 = wx.FlexGridSizer(2, 1, 0, 0)
label_2 = wx.StaticText(self, wx.ID_ANY, "You have selected:")
grid_sizer_2.Add(label_2, 0, wx.LEFT | wx.RIGHT | wx.TOP, 10)
grid_sizer_2.Add(self.txt_result, 0, wx.ALL | wx.EXPAND, 10)
self.SetSizer(grid_sizer_2)
grid_sizer_2.Fit(self)
grid_sizer_2.AddGrowableCol(0)
self.Layout()
def SetParent(self, parent):
self._parent = parent
The trick is in the Panel1's (btn_send) button event handler:
# self._parent is a reference to MainFrame
# panel2 is a main_frame's element.
# txt_result is a TextCtrl in Panel2 class.
self._parent.panel2.txt_result.SetValue(selectedFruit)

Drag and Drop files to diferent WxFilePicker controls

I'm creating a GUI where I need to drag and drop different files into different wxFilePicker controls.
Based on this example wxPython: Dragging a file into window to get file path I was able to get the filepath from one file into one filepicker or that same filepath for all filepickers with:
def updateText(self, text):
"""
Write text to the wx control
"""
self.m_filePicker1.SetPath(text)
self.m_filePicker2.SetPath(text)
self.m_filePicker3.SetPath(text)
Although it worked as it would be expected that solution its not useful.
Do I need to create different panels for each wxfilepicker control or there is another way to solve this problem?
Thank you in advance
Ivo
Here's the code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import wx
import wx.xrc
########################################################################
class MyFileDropTarget(wx.FileDropTarget):
""""""
#----------------------------------------------------------------------
def __init__(self, window):
"""Constructor"""
wx.FileDropTarget.__init__(self)
self.window = window
#----------------------------------------------------------------------
def OnDropFiles(self, x, y, filenames):
"""
When files are dropped, write where they were dropped and then
the file paths themselves
"""
#self.window.SetInsertionPointEnd()
self.window.updateText("\n%d file(s) dropped at %d,%d:\n" %
(len(filenames), x, y))
print filenames
for filepath in filenames:
self.window.updateText(filepath + '\n')
class Test_Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(500, 164),
style=wx.TAB_TRAVERSAL)
bSizer122 = wx.BoxSizer(wx.VERTICAL)
sbSizer2 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, u"Model"), wx.VERTICAL)
bSizer3 = wx.BoxSizer(wx.VERTICAL)
bSizer4 = wx.BoxSizer(wx.VERTICAL)
bSizer10 = wx.BoxSizer(wx.HORIZONTAL)
bSizer12 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText1 = wx.StaticText(sbSizer2.GetStaticBox(), wx.ID_ANY, u"Model File", wx.DefaultPosition,
wx.Size(100, -1), wx.ALIGN_CENTRE)
self.m_staticText1.Wrap(-1)
bSizer12.Add(self.m_staticText1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer12, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer13 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker1 = wx.FilePickerCtrl(sbSizer2.GetStaticBox(), wx.ID_ANY, wx.EmptyString, u"Select a file",
u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer13.Add(self.m_filePicker1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer13, 1, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, 5)
bSizer4.Add(bSizer10, 0, wx.EXPAND, 5)
bSizer11 = wx.BoxSizer(wx.HORIZONTAL)
bSizer14 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText2 = wx.StaticText(sbSizer2.GetStaticBox(), wx.ID_ANY, u"Data File", wx.DefaultPosition,
wx.Size(100, -1), wx.ALIGN_CENTRE)
self.m_staticText2.Wrap(-1)
bSizer14.Add(self.m_staticText2, 1, wx.ALL, 5)
bSizer11.Add(bSizer14, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer15 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker2 = wx.FilePickerCtrl(sbSizer2.GetStaticBox(), wx.ID_ANY, wx.EmptyString, u"Select a file",
u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer15.Add(self.m_filePicker2, 0, wx.ALL | wx.EXPAND, 5)
bSizer11.Add(bSizer15, 1, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer4.Add(bSizer11, 1, wx.EXPAND, 5)
bSizer111 = wx.BoxSizer(wx.HORIZONTAL)
bSizer141 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText21 = wx.StaticText(sbSizer2.GetStaticBox(), wx.ID_ANY, u"DoF Labels", wx.DefaultPosition,
wx.Size(100, -1), wx.ALIGN_CENTRE)
self.m_staticText21.Wrap(-1)
bSizer141.Add(self.m_staticText21, 1, wx.ALL | wx.EXPAND, 5)
bSizer111.Add(bSizer141, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer151 = wx.BoxSizer(wx.VERTICAL)
file_drop_target = MyFileDropTarget(self)
self.m_filePicker3 = wx.FilePickerCtrl(sbSizer2.GetStaticBox(), wx.ID_ANY, wx.EmptyString, u"Select a file",
u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer151.Add(self.m_filePicker3, 0, wx.ALL | wx.EXPAND, 5)
bSizer111.Add(bSizer151, 1, wx.EXPAND, 5)
bSizer4.Add(bSizer111, 0, wx.EXPAND, 5)
bSizer3.Add(bSizer4, 0, wx.EXPAND, 5)
sbSizer2.Add(bSizer3, 1, wx.EXPAND, 5)
bSizer122.Add(sbSizer2, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizer(bSizer122)
self.Layout()
self.m_filePicker1.SetDropTarget(file_drop_target)
def updateText(self, text):
"""
Write text to the wx control
"""
self.m_filePicker1.SetPath(text)
def __del__(self):
pass
class Test_Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
size=wx.Size(500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
bSizer239 = wx.BoxSizer(wx.VERTICAL)
self.m_panel8 = Test_Panel(self)#, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
#self.m_panel8 = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
bSizer239.Add(self.m_panel8, 1, wx.EXPAND
, 5)
self.SetSizer(bSizer239)
self.Layout()
self.Centre(wx.BOTH)
def __del__(self):
pass
if __name__ == "__main__":
app = wx.App(redirect=False)
frame = Test_Frame(None)
app.SetTopWindow(frame)
frame.Show(True)
app.MainLoop()
Here's my solution for the problem:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.xrc
class MyFileDropTarget(wx.FileDropTarget):
""""""
#----------------------------------------------------------------------
def __init__(self, window):
"""Constructor"""
wx.FileDropTarget.__init__(self)
self.window = window
#----------------------------------------------------------------------
def OnDropFiles(self, x, y, filenames):
"""
When files are dropped, write where they were dropped and then
the file paths themselves
"""
#self.window.SetInsertionPointEnd()
self.window.updateText("\n%d file(s) dropped at %d,%d:\n" %
(len(filenames), x, y))
# print filenames
for filepath in filenames:
# self.window.updateText(filepath + '\n')
self.window.updateText(filepath)
class Model_Data(wx.Panel):
def __init__(self, parent, label):
wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(-1, -1),
style=wx.TAB_TRAVERSAL)
bSizer122 = wx.BoxSizer(wx.VERTICAL)
bSizer3 = wx.BoxSizer(wx.VERTICAL)
bSizer10 = wx.BoxSizer(wx.HORIZONTAL)
bSizer12 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, label, wx.DefaultPosition, wx.Size(100, -1),
#self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"Model File", wx.DefaultPosition, wx.Size(100, -1),
wx.ALIGN_CENTRE)
self.m_staticText1.Wrap(-1)
bSizer12.Add(self.m_staticText1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer12, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer13 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker1 = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*",
wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer13.Add(self.m_filePicker1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer13, 1, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, 5)
##############################################
file_drop_target = MyFileDropTarget(self)
self.m_filePicker1.SetDropTarget(file_drop_target)
##############################################
bSizer3.Add(bSizer10, 0, wx.EXPAND, 5)
bSizer122.Add(bSizer3, 0, wx.EXPAND, 5)
self.SetSizer(bSizer122)
self.Layout()
def updateText(self, text):
"""
Write text to the wx control
"""
self.m_filePicker1.SetPath(text)
def __del__(self):
pass
class Generic_Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(-1, -1),
style=wx.TAB_TRAVERSAL)
bSizer122 = wx.BoxSizer(wx.VERTICAL)
bSizer3 = wx.BoxSizer(wx.VERTICAL)
bSizer10 = wx.BoxSizer(wx.HORIZONTAL)
bSizer12 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"Model File", wx.DefaultPosition, wx.Size(100, -1),
wx.ALIGN_CENTRE)
self.m_staticText1.Wrap(-1)
bSizer12.Add(self.m_staticText1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer12, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer13 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker1 = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*",
wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer13.Add(self.m_filePicker1, 1, wx.ALL, 5)
bSizer10.Add(bSizer13, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer3.Add(bSizer10, 1, wx.EXPAND, 5)
bSizer122.Add(bSizer3, 1, wx.EXPAND, 5)
self.SetSizer(bSizer122)
self.Layout()
bSizer122.Fit(self)
def __del__(self):
pass
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
size=wx.Size(500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetBackgroundColour('LightGrey')
self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
bSizer239 = wx.BoxSizer(wx.VERTICAL)
sbSizer8 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, u"Model "), wx.VERTICAL)
self.m_panel1 = Model_Data(sbSizer8.GetStaticBox(),'Model File')
sbSizer8.Add(self.m_panel1, 0, wx.EXPAND | wx.ALL, 5)
self.m_panel2 = Model_Data(sbSizer8.GetStaticBox(),'Model Data')
sbSizer8.Add(self.m_panel2, 0, wx.ALL | wx.EXPAND, 5)
self.m_panel3 = Model_Data(sbSizer8.GetStaticBox(),'DoF Labels')
# wx.Panel(sbSizer8.GetStaticBox(), wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
# wx.TAB_TRAVERSAL)
sbSizer8.Add(self.m_panel3, 0, wx.EXPAND | wx.ALL, 5)
bSizer239.Add(sbSizer8, 1, wx.ALL | wx.EXPAND, 5)
self.SetSizer(bSizer239)
self.Layout()
self.Centre(wx.BOTH)
def __del__(self):
pass
if __name__ == "__main__":
app = wx.App(redirect=False)
frame = Frame(None)
app.SetTopWindow(frame)
frame.Show(True)
app.MainLoop()

How to handling event in wxpython from another class in separate file?

I use wxGlade to create GUI, it's a nice GUI builder.
I want to collect all the events that I made in MyFrame.py file in a separate file called event.py,. But when I run my app from event.py it doesnt show Dialog when I click "Pilihan Lainnya". Below is MyFrame.py file.
# generated by wxGlade 9a4613e7dab8 on Sun Apr 16 17:54:01 2017
#
import wx
# begin wxGlade: dependencies
import wx.grid
# 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_menubar = wx.MenuBar()
wxglade_tmp_menu = wx.Menu()
wxglade_tmp_menu.Append(wx.ID_ANY, "item", "")
wxglade_tmp_menu.Append(wx.ID_ANY, "item", "")
self.frame_menubar.Append(wxglade_tmp_menu, "item")
wxglade_tmp_menu = wx.Menu()
wxglade_tmp_menu.Append(wx.ID_ANY, "item", "")
wxglade_tmp_menu.Append(wx.ID_ANY, "item", "")
self.frame_menubar.Append(wxglade_tmp_menu, "item")
wxglade_tmp_menu = wx.Menu()
wxglade_tmp_menu.Append(wx.ID_ANY, "item", "")
self.frame_menubar.Append(wxglade_tmp_menu, "item")
self.SetMenuBar(self.frame_menubar)
# Menu Bar end
self.frame_statusbar = self.CreateStatusBar(1)
# Tool Bar
self.frame_toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL | wx.TB_NOICONS | wx.TB_TEXT)
self.SetToolBar(self.frame_toolbar)
self.frame_toolbar.AddLabelTool(wx.ID_ANY, "item", wx.NullBitmap, wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.frame_toolbar.AddLabelTool(wx.ID_ANY, "item", wx.NullBitmap, wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.frame_toolbar.AddLabelTool(wx.ID_ANY, "item", wx.NullBitmap, wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.frame_toolbar.AddLabelTool(wx.ID_ANY, "item", wx.NullBitmap, wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.frame_toolbar.AddLabelTool(wx.ID_ANY, "item", wx.NullBitmap, wx.NullBitmap, wx.ITEM_NORMAL, "", "")
# Tool Bar end
self.notebook_1 = wx.Notebook(self, wx.ID_ANY, style=wx.NB_LEFT)
self.Menu_Utama = wx.Panel(self.notebook_1, wx.ID_ANY)
self.Penjualan = wx.Panel(self.notebook_1, wx.ID_ANY)
self.panel_3 = wx.Panel(self.Penjualan, wx.ID_ANY)
self.text_ctrl_1 = wx.TextCtrl(self.Penjualan, wx.ID_ANY, "")
self.text_ctrl_2 = wx.TextCtrl(self.Penjualan, wx.ID_ANY, "1", style=wx.TE_CENTRE)
self.button_2 = wx.Button(self.Penjualan, wx.ID_ADD, "")
self.list_ctrl_1 = wx.ListCtrl(self.Penjualan, wx.ID_ANY, style=wx.BORDER_DEFAULT | wx.BORDER_SUNKEN | wx.LC_REPORT | wx.NO_FULL_REPAINT_ON_RESIZE)
self.button_3 = wx.Button(self.Penjualan, wx.ID_ANY, "Cetak & Simpan")
self.button_1 = wx.Button(self.Penjualan, wx.ID_ANY, "Cetak Saja")
self.button_4 = wx.Button(self.Penjualan, wx.ID_ANY, "Hapus")
self.button_5 = wx.Button(self.Penjualan, wx.ID_ANY, "Lihat")
self.button_6 = wx.Button(self.Penjualan, wx.ID_ANY, "Pilihan Lainnya", style=wx.BU_EXACTFIT)
self.Data_Barang = wx.Panel(self.notebook_1, wx.ID_ANY)
self.grid_1 = wx.grid.Grid(self.Data_Barang, wx.ID_ANY, size=(1, 1))
self.Data_Pekerja = wx.Panel(self.notebook_1, wx.ID_ANY)
self.Pengaturan = wx.Panel(self.notebook_1, wx.ID_ANY)
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_BUTTON, self.Input, self.button_2)
self.Bind(wx.EVT_BUTTON, self.pilihan_lainnya, self.button_6)
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame")
self.SetSize((1360, 737))
self.frame_statusbar.SetStatusWidths([-1])
# statusbar fields
frame_statusbar_fields = ["Status"]
for i in range(len(frame_statusbar_fields)):
self.frame_statusbar.SetStatusText(frame_statusbar_fields[i], i)
self.frame_toolbar.Realize()
self.panel_3.SetBackgroundColour(wx.Colour(255, 255, 0))
self.text_ctrl_1.SetMinSize((200, 34))
self.button_3.SetMinSize((150, 34))
self.button_1.SetMinSize((150, 34))
self.button_4.SetMinSize((150, 34))
self.button_5.SetMinSize((150, 34))
self.button_6.SetMinSize((150, 34))
self.grid_1.CreateGrid(10, 5)
self.grid_1.EnableEditing(0)
self.grid_1.SetColLabelValue(0, "KODE BARANG")
self.grid_1.SetColSize(0, 150)
self.grid_1.SetColLabelValue(1, "NAMA BARANG")
self.grid_1.SetColSize(1, 300)
self.grid_1.SetColLabelValue(2, "SATUAN")
self.grid_1.SetColSize(2, 75)
self.grid_1.SetColLabelValue(3, " HARGA\n SATUAN")
self.grid_1.SetColSize(3, 100)
self.grid_1.SetColLabelValue(4, "KETERANGAN")
self.grid_1.SetColSize(4, 300)
self.grid_1.SetFocus()
self.Data_Barang.SetBackgroundColour(wx.Colour(159, 159, 95))
self.Data_Pekerja.SetBackgroundColour(wx.Colour(159, 159, 95))
self.Pengaturan.SetBackgroundColour(wx.Colour(159, 159, 95))
self.notebook_1.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_3 = wx.BoxSizer(wx.VERTICAL)
sizer_8 = wx.BoxSizer(wx.VERTICAL)
sizer_4 = wx.BoxSizer(wx.VERTICAL)
sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
grid_sizer_2 = wx.GridSizer(5, 1, 0, 0)
sizer_6 = wx.BoxSizer(wx.VERTICAL)
grid_sizer_1 = wx.FlexGridSizer(1, 7, 0, 0)
sizer_7 = wx.BoxSizer(wx.VERTICAL)
label_2 = wx.StaticText(self, wx.ID_ANY, "APLIKASI PENJUALAN KASIR CANGGIH")
label_2.SetFont(wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
sizer_3.Add(label_2, 0, wx.ALIGN_CENTER, 0)
sizer_6.Add((0, 0), 0, 0, 0)
label_5 = wx.StaticText(self.panel_3, wx.ID_ANY, "0", style=wx.ALIGN_CENTER | wx.ALIGN_RIGHT)
label_5.SetFont(wx.Font(50, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
sizer_7.Add(label_5, 1, wx.ALIGN_RIGHT, 0)
self.panel_3.SetSizer(sizer_7)
sizer_6.Add(self.panel_3, 1, wx.EXPAND, 0)
label_1 = wx.StaticText(self.Penjualan, wx.ID_ANY, "Kode Barang")
grid_sizer_1.Add(label_1, 0, wx.ALIGN_CENTER | wx.LEFT, 8)
grid_sizer_1.Add(self.text_ctrl_1, 0, wx.LEFT | wx.RIGHT, 8)
label_3 = wx.StaticText(self.Penjualan, wx.ID_ANY, "Jumlah")
grid_sizer_1.Add(label_3, 0, wx.ALIGN_CENTER, 0)
grid_sizer_1.Add(self.text_ctrl_2, 0, wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT, 8)
grid_sizer_1.Add(self.button_2, 0, wx.RIGHT, 8)
sizer_6.Add(grid_sizer_1, 0, wx.EXPAND, 0)
sizer_4.Add(sizer_6, 1, wx.EXPAND, 0)
sizer_5.Add(self.list_ctrl_1, 5, wx.EXPAND, 0)
grid_sizer_2.Add(self.button_3, 0, 0, 0)
grid_sizer_2.Add(self.button_1, 0, 0, 0)
grid_sizer_2.Add(self.button_4, 0, 0, 0)
grid_sizer_2.Add(self.button_5, 0, 0, 0)
grid_sizer_2.Add(self.button_6, 0, 0, 0)
sizer_5.Add(grid_sizer_2, 0, wx.LEFT | wx.RIGHT, 10)
sizer_4.Add(sizer_5, 4, wx.EXPAND, 0)
self.Penjualan.SetSizer(sizer_4)
sizer_8.Add((0, 0), 0, 0, 0)
sizer_8.Add(self.grid_1, 3, wx.EXPAND, 0)
self.Data_Barang.SetSizer(sizer_8)
self.notebook_1.AddPage(self.Menu_Utama, "Menu Utama")
self.notebook_1.AddPage(self.Penjualan, "Penjualan")
self.notebook_1.AddPage(self.Data_Barang, "Data Barang")
self.notebook_1.AddPage(self.Data_Pekerja, "Data Pekerja")
self.notebook_1.AddPage(self.Pengaturan, "Pengaturan")
sizer_3.Add(self.notebook_1, 5, wx.ALIGN_CENTER | wx.EXPAND, 0)
self.SetSizer(sizer_3)
self.Layout()
self.Centre()
self.SetSize((1360, 737))
# end wxGlade
def Input(self, event): # wxGlade: MyFrame.<event_handler>
print("Event handler 'Input' not implemented!")
event.Skip()
def pilihan_lainnya(self, event): # wxGlade: MyFrame.<event_handler>
print("Event handler 'pilihan_lainnya' not implemented!")
print("I want to make the event could be triggered from event.py")
event.Skip()
# end of class MyFrame
this is my other file named app.py
# generated by wxGlade 9a4613e7dab8 on Sun Apr 16 17:59:07 2017
# This is an automatically generated file.
# Manual changes will be overwritten without warning!
import wx
from MyFrame import MyFrame
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(frame)
frame.Show()
return True
# end of class MyApp
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
This is the file named MyDialog.py. This is the Dialog Frame that I want to show if I press the button from the main Frame.
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 9a4613e7dab8 on Sat Apr 15 11:29:39 2017
#
import wx
# begin wxGlade: dependencies
# end wxGlade
# begin wxGlade: extracode
# end wxGlade
class MyDialog(wx.Dialog):
def __init__(self, *args, **kwds):
# begin wxGlade: MyDialog.__init__
kwds["style"] = wx.DEFAULT_DIALOG_STYLE
wx.Dialog.__init__(self, *args, **kwds)
self.button_7 = wx.Button(self, wx.ID_ANY, "button_7")
self.button_8 = wx.Button(self, wx.ID_ANY, "button_7")
self.button_9 = wx.Button(self, wx.ID_ANY, "button_7")
self.button_10 = wx.Button(self, wx.ID_ANY, "button_7")
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyDialog.__set_properties
self.SetTitle("dialog")
self.button_7.SetMinSize((125, 64))
self.button_8.SetMinSize((125, 64))
self.button_9.SetMinSize((125, 64))
self.button_10.SetMinSize((125, 64))
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyDialog.__do_layout
sizer_2 = wx.BoxSizer(wx.VERTICAL)
grid_sizer_3 = wx.FlexGridSizer(2, 2, 0, 0)
label_4 = wx.StaticText(self, wx.ID_ANY, "Pilihan anda ?")
sizer_2.Add(label_4, 0, wx.ALIGN_CENTER, 0)
grid_sizer_3.Add(self.button_7, 0, wx.ALL, 5)
grid_sizer_3.Add(self.button_8, 0, wx.ALL, 5)
grid_sizer_3.Add(self.button_9, 0, wx.ALL, 5)
grid_sizer_3.Add(self.button_10, 0, wx.ALL, 5)
sizer_2.Add(grid_sizer_3, 2, wx.EXPAND, 0)
self.SetSizer(sizer_2)
sizer_2.Fit(self)
self.Layout()
# end wxGlade
# end of class MyDialog
and this is my last file named event.py , All I want is to have the event handler being invoked by this file. I can't do it. I know I can use wxformbuilder and could handle this by using the feature of inherited class. But I want to use wxGlade cause this tool is more flexible and fit to my needs except for this one :-). Please help me`
#!usr/bin/env python
# -*- coding: utf-8 -*-
#import MyFrame
import MyDialog
import wx
import app
class AplikasiRun(app.MyApp):
def __init__(self):
super(AplikasiRun,self).__init__()
def pilihan_lainnya(self,event):
print "this is working"
self.buka = MyDialog.MyDialog(None, wx.ID_ANY, "")
def sukasuka():
###I don't use this for now ###
pass
t = AplikasiRun()
t.MainLoop()
This piece of code:
self.Bind(wx.EVT_BUTTON, self.pilihan_lainnya, self.button_6)
should be written as:
self.button_6.Bind(wx.EVT_BUTTON, self.pilihan_lainnya, self.button_6)
I'm actually surprised that this was compiled and run... EVT_BUTTON should be bound to buttons and not frames.

Why does this wxPython GUI freeze when self.Destroy() is used instead of self.Close()? (example attached)

LoginDialog is opened with self.loginButton, and upon closing it, the GUI freezes; the login button remains pressed, and there are only alert sounds when attempting to click on anything.
LoginDialog() was from this tutorial, just augmented with the file-write line in onLogin (which isn't the problem source). All appears to work when switching from self.Destroy() to self.Close().
wxPython version is 3.0, Python 2.7.10
The following code is a working example of the issue:
import wx
# wxGlade dependency
import gettext
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.updateLoginButton = wx.Button(self, wx.ID_ANY, _("Store/Update Login"))
self.Bind(wx.EVT_BUTTON, self.updateLogin, self.updateLoginButton)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle(_("Dialog Test"))
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
grid_sizer_1 = wx.FlexGridSizer(3, 1, 1, 0)
exportButtons_sizer = wx.FlexGridSizer(1, 1, 1, 10)
exportButtons_sizer.Add(self.updateLoginButton, 0, wx.TOP | wx.ALIGN_CENTER, 15)
grid_sizer_1.Add(exportButtons_sizer, 0, wx.TOP | wx.ALIGN_CENTER, 20)
sizer_1.Add(grid_sizer_1, 1, wx.ALL | wx.EXPAND, 15)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
def updateLogin(self, event):
dlg = LoginDialog(self, -1)
dlg.ShowModal()
class MyApp(wx.App):
def OnInit(self):
frame_1 = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(frame_1)
frame_1.Show()
return True
class LoginDialog(wx.Dialog):
"""
Class to define login dialog
"""
# ----------------------------------------------------------------------
def __init__(self, parent, id, title="Update Login Info"):
"""Constructor"""
wx.Dialog.__init__(self, parent, id, title)
# user info
user_sizer = wx.BoxSizer(wx.HORIZONTAL)
user_lbl = wx.StaticText(self, label="Username:")
user_sizer.Add(user_lbl, 0, wx.ALL | wx.CENTER, 5)
self.user = wx.TextCtrl(self)
user_sizer.Add(self.user, 0, wx.ALL, 5)
# pass info
p_sizer = wx.BoxSizer(wx.HORIZONTAL)
p_lbl = wx.StaticText(self, label="Password:")
p_sizer.Add(p_lbl, 0, wx.ALL | wx.CENTER, 5)
self.password = wx.TextCtrl(self, style=wx.TE_PASSWORD | wx.TE_PROCESS_ENTER)
p_sizer.Add(self.password, 0, wx.ALL, 5)
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(user_sizer, 0, wx.ALL, 5)
main_sizer.Add(p_sizer, 0, wx.ALL, 5)
btn = wx.Button(self, label="OK")
btn.Bind(wx.EVT_BUTTON, self.onLogin)
main_sizer.Add(btn, 0, wx.ALL | wx.CENTER, 5)
self.SetSizer(main_sizer)
self.__set_properties()
# ----------------------------------------------------------------------
def onLogin(self, event):
"""
Check credentials and login
"""
password = self.password.GetValue()
email = self.user.GetValue()
with open('login.txt', 'w') as f:
f.write(email + ', ' + password)
self.Destroy()
def __set_properties(self):
self.user.SetMinSize((220, 20))
if __name__ == "__main__":
gettext.install("app")
app = MyApp(0)
app.MainLoop()
If a dialog is intended to be used with ShowModal dialog then the dialog should call EndModal when it is done, and the caller should be the one who calls Destroy after it has fetched values from the dialog if needed.

Categories

Resources