How to add Hotkeys in a text editor? - python

There is a simple text editor developed with WxPython.
I need to add hot keys. ctrl+1, ctrl+2, ctrl+3 and ctrl+4. So that when you press these hot keys to perform certain functions.
How can I do it?
The code:
import os
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title)
self.textBox = wx.TextCtrl(self, style=wx.TE_MULTILINE)
fileMenu = wx.Menu()
menuOpen = fileMenu.Append(wx.ID_OPEN,"O&pen","")
menuExit = fileMenu.Append(wx.ID_EXIT,"E&xit","")
menuBar = wx.MenuBar()
menuBar.Append(fileMenu,"&File")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Show(True)
def OnOpen(self,e):
self.dirName = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirName, "", "text file|*.txt", wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.fileName = dlg.GetFilename()
self.dirName = dlg.GetDirectory()
f = open(os.path.join(self.dirName, self.fileName), 'r')
self.textBox.SetValue(f.read())
f.close()
dlg.Destroy()
def OnExit(self,e):
self.Close(True)
app = wx.App(False)
frame = MyFrame(None, 'RecordBooks')
app.MainLoop()

Related

How to add a submenu item to a popup menu item in wxPython?

I want to add a few submenu items to an existing popup menu item. I found several examples on how to create a submenu for a standard menu, but none about creating a submenu for a popup menu.
I tried to apply to the popup menu the same logic as for a standard menu, but failed to do that properly (while at the same time I have no trouble in creating a standard menu with submenus).
In a portion of code like the one below, how to add a submenu ?
# ...
class ButtonContext(wx.Menu):
def __init__(self, parent):
super(ButtonContext, self).__init__()
self.parent = parent
button_popup = wx.MenuItem(self, wx.ID_ANY, 'test popup')
self.Append(button_popup)
self.Bind(wx.EVT_MENU, self.button_action, button_popup)
def button_action(self, event):
event.Skip()
# ...
This will work, in fact popup menus are based on wx.Menu class so the logic and the behaviour are the same
class ButtonContext(wx.Menu):
def __init__(self, parent):
super(ButtonContext, self).__init__()
self.parent = parent
button_popup = wx.MenuItem(self, wx.ID_ANY, 'test popup')
submenu_popup = wx.Menu()
submenu_button_popup = wx.MenuItem(self, wx.ID_ANY, 'test submenu popup')
submenu_popup.Append(submenu_button_popup)
self.AppendSubMenu(submenu_popup, "submenu")
self.Append(button_popup)
self.Bind(wx.EVT_MENU, self.button_action, button_popup)
Just to illustrate the SetSubMenu(menu) method which can also be used to create a sub-menu in this instance.
import wx
class MyFrame(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((400, 300))
self.SetTitle("test frame")
sizer_0 = wx.BoxSizer(wx.HORIZONTAL)
self.window_1 = wx.SplitterWindow(self, wx.ID_ANY)
self.window_1.SetMinimumPaneSize(20)
sizer_0.Add(self.window_1, 1, wx.EXPAND, 0)
self.pane_1 = wx.Panel(self.window_1, wx.ID_ANY)
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
self.pane_2 = wx.Panel(self.window_1, wx.ID_ANY)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
self.button = wx.Button(self.pane_2, wx.ID_ANY, "test button")
sizer_2.Add(self.button, 0, 0, 0)
self.pane_1.SetSizer(sizer_1)
self.pane_2.SetSizer(sizer_2)
self.window_1.SplitVertically(self.pane_1, self.pane_2)
self.SetSizer(sizer_0)
self.Layout()
self.Bind(wx.EVT_CONTEXT_MENU, self.OnButtonContextMenu, self.button)
self.Show()
def OnButtonContextMenu(self, event):
self.PopupMenu(ButtonContext(self))
class ButtonContext(wx.Menu):
def __init__(self, parent):
super(ButtonContext, self).__init__()
self.parent = parent
button_popup = wx.MenuItem(self, wx.ID_ANY, 'test popup')
self.Append(button_popup)
submenu = wx.Menu()
submenu1 = wx.MenuItem(submenu, wx.ID_ANY, 'Submenu 1')
submenu2 = wx.MenuItem(submenu, wx.ID_ANY, 'Submenu 2')
submenu3 = wx.MenuItem(submenu, wx.ID_ANY, 'Submenu 3')
submenu.Append(submenu1)
submenu.Append(submenu2)
submenu.Append(submenu3)
button_popup2 = wx.MenuItem(self, wx.ID_ANY, 'A sub menu')
button_popup2.SetSubMenu(submenu)
self.Append(button_popup2)
self.Bind(wx.EVT_MENU, self.button_action, button_popup)
self.Bind(wx.EVT_MENU, self.submenu1_action, submenu1)
def button_action(self, event):
print("Test Menu Button")
event.Skip()
def submenu1_action(self, event):
print("Submenu 1")
event.Skip()
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, wx.ID_ANY, "")
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()

wxPython how do I retrieve information from a dialog box opened by a button

I'm trying to pull data from a text entry dialog box that's opened by a button click event using wxpython using this code.
import wx
class apple(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'PyLabs', size=(840,600))
panel = wx.Panel(self)
box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi')
status_bar = self.CreateStatusBar()
menu_bar = wx.MenuBar()
options_menu = wx.Menu()
options_menu.Append(wx.NewId(), "Settings", "OpenSettings...")
options_menu.Append(wx.NewId(), "Advanced", "Check Advanced...")
menu_bar.Append(options_menu, "Options")
self.SetMenuBar(menu_bar)
New_Experiment_Button = wx.Button(panel, pos=(10,10), label='New Experiment', size=(120, 40))
answer = self.Bind(wx.EVT_BUTTON, self.openFrame, New_Experiment_Button)
print(answer)
def openFrame(self, event):
box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi')
if box.ShowModal() == wx.ID_OK:
answer = str(box.getValue)
event.Skip()
return answer
if __name__=='__main__':
app=wx.PySimpleApp()
frame = apple(parent=None, id=-1)
frame.Show()
app.MainLoop()
I'm extremely new to wxpython coding and i don't understand how i'm to grab the data pulled by the button event when it's called from inside the Bind() function.
The output of print(answer) is "None"
If anybody could help me with this it'd be greatly appreciated!
Why not just have the print in the function, why would you need to return it to def __init__ ?
def openFrame(self, event):
box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi')
if box.ShowModal() == wx.ID_OK:
answer = box.GetValue()
event.Skip()
print answer
The "answer" is box.GetValue(), however you have a few more issues with your experiment, see below, check the differences
Edited with reference to your comments, store you result in a self variable and see the difference in the Bind statements on the buttons.
import wx
class apple(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'PyLabs', size=(840,600))
panel = wx.Panel(self)
status_bar = self.CreateStatusBar()
menu_bar = wx.MenuBar()
options_menu = wx.Menu()
options_menu.Append(wx.NewId(), "Settings", "OpenSettings...")
options_menu.Append(wx.NewId(), "Advanced", "Check Advanced...")
menu_bar.Append(options_menu, "Options")
self.SetMenuBar(menu_bar)
New_Experiment_Button = wx.Button(panel, pos=(10,10), label='New Experiment', size=(130, 30))
New_Experiment_Button.Bind(wx.EVT_BUTTON, self.openFrame)
Result_Button = wx.Button(panel, pos=(10,60), label='Result of Experiment', size=(130, 30))
Result_Button.Bind(wx.EVT_BUTTON, self.resultValue)
self.Text = wx.TextCtrl(panel, -1, "Nothing yet", pos=(10,100), size=(200,30))
def openFrame(self, event):
box = wx.TextEntryDialog(None, 'What is your answer', 'Heading','Hi')
if box.ShowModal() == wx.ID_OK:
self.answer = box.GetValue()
box.Destroy()
def resultValue(self, event):
try:
self.Text.SetValue(self.answer)
except:
self.Text.SetValue("No answer yet supplied")
if __name__=='__main__':
app=wx.App()
frame = apple(parent=None, id=-1)
frame.Show()
app.MainLoop()

wxpython phoenix says that Frame init args are wrong

I am trying to create simple window with menu in wx python 3.0.4.
Actually I get an error:
wx.Frame.init(self, ID_ANY, "Title", DefaultPosition, (350,200), DEFAULT_FRAME_STYLE, FrameNameStr) TypeError: Frame():
arguments did not match any overloaded call: overload 1: too many
arguments overload 2: argument 1 has unexpected type 'StandardID'
Even this code is from documentation. Could someone tell me what am I doing wrong, please?
import wx
from wx import *
class MainWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, ID_ANY, "Title", DefaultPosition, (350,200), DEFAULT_FRAME_STYLE, FrameNameStr)
self.Bind(wx.EVT_CLOSE, self.OnClose)
menuBar = wx.MenuBar()
menu = wx.Menu()
m_exit = menu.Append(wx.ID_EXIT, "E&xit", "Close window and exit program.")
self.Bind(wx.EVT_MENU, self.OnClose, m_exit)
menuBar.Append(menu, "&File")
menu = wx.Menu()
m_about = menu.Append(wx.ID_ABOUT, "&About", "Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, m_about)
menuBar.Append(menu, "&Help")
self.SetMenuBar(menuBar)
self.main_panel = MainPanel(self)
def OnClose(self, e):
self.Close()
def OnAbout(self, event):
dlg = AboutBox(self)
dlg.ShowModal()
dlg.Destroy()
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
class AboutBox(wx.MessageDialog):
def __init__(self, parent):
wx.MessageDialog.__init__(parent, "About", "About", wx.OK | wx.ICON_INFORMATION, pos=DefaultPosition)
self.CentreOnParent(wx.BOTH)
self.SetFocus()
if __name__ == "__main__":
app = wx.App(False)
frame = MainWindow()
frame.Show()
app.MainLoop()
Frame(parent, id=ID_ANY, title="", pos=DefaultPosition,
size=DefaultSize, style=DEFAULT_FRAME_STYLE, name=FrameNameStr)
You missed the parent argument.
Working code
import wx
class MainWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(
self, None, wx.ID_ANY, "Title", wx.DefaultPosition, (350,200),
wx.DEFAULT_FRAME_STYLE, wx.FrameNameStr)
self.Bind(wx.EVT_CLOSE, self.OnClose)
menuBar = wx.MenuBar()
menu = wx.Menu()
m_exit = menu.Append(
wx.ID_EXIT, "E&xit", "Close window and exit program.")
self.Bind(wx.EVT_MENU, self.OnClose, m_exit)
menuBar.Append(menu, "&File")
menu = wx.Menu()
m_about = menu.Append(
wx.ID_ABOUT, "&About", "Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, m_about)
menuBar.Append(menu, "&Help")
self.SetMenuBar(menuBar)
self.main_panel = MainPanel(self)
def OnClose(self, e):
self.Destroy()
def OnAbout(self, event):
dlg = AboutBox(self)
dlg.ShowModal()
dlg.Destroy()
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
class AboutBox(wx.MessageDialog):
def __init__(self, parent):
wx.MessageDialog.__init__(
self, parent, "About", "About", wx.OK | wx.ICON_INFORMATION,
pos=wx.DefaultPosition)
self.CentreOnParent(wx.BOTH)
if __name__ == "__main__":
app = wx.App(False)
frame = MainWindow()
frame.Show()
app.MainLoop()

unable to quit wxpython

So I think I know what my problem is but I cant seem to figure out how to fix it. I am relatively new to wxPython. I am moving some functionality I have in a terminal script to a GUI and cant seem to get it right. I use anaconda for my python distribution and have added wxPython for the GUI. I want users to be able to drag and drop files into the text controls and then have the file contents imported into dataframes for analysis with pandas. So far everything is happy. Other than the fact that the program will not quit. I think it has to do with how I am defining the window and the frame. I have removed a significant amount of the functionality from the script to help simplify things. Please let me know what I am missing.
Thanks
Tyler
import wx
import os
#import pandas as pd
#import numpy as np
#import matplotlib.pyplot as ply
#from scipy.stats import linregress
class MyFileDropTarget(wx.FileDropTarget):
#----------------------------------------------------------------------
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
#----------------------------------------------------------------------
def OnDropFiles(self, x, y, filenames):
self.window.SetInsertionPointEnd(y)
#self.window.updateText("\n%d file(s) dropped at %d,%d:\n" %
# (len(filenames), x, y), y)
for filepath in filenames:
self.window.updateText(filepath + '\n', y)
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
file_drop_target = MyFileDropTarget(self)
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Creating the menubar.
menubar = wx.MenuBar()
fileMenu = wx.Menu()
helpMenu = wx.Menu()
menubar.Append(fileMenu, '&File')
menuOpen = fileMenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
#self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
fileMenu.AppendSeparator()
menuExit = fileMenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
menubar.Append(helpMenu, '&Help')
menuAbout= helpMenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.SetMenuBar(menubar)
#Create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
grid = wx.GridBagSizer(hgap=5, vgap=5)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
#Create a button
self.button = wx.Button(self, label="Test")
#self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
# Radio Boxes
sysList = ['QEXL','QEX10','QEX7']
wlList = ['1100', '1400', '1800']
sys = wx.RadioBox(self, label="What system are you calibrating ?", pos=(20, 40), choices=sysList, majorDimension=3,
style=wx.RA_SPECIFY_COLS)
grid.Add(sys, pos=(1,0), span=(1,3))
WL = wx.RadioBox(self, label="Maximum WL you currently Calibrating ?", pos=(20, 100), choices=wlList, majorDimension=0,
style=wx.RA_SPECIFY_COLS)
grid.Add(WL, pos=(2,0), span=(1,3))
self.lblname = wx.StaticText(self, label="Cal File 1 :")
grid.Add(self.lblname, pos=(3,0))
self.Cal_1 = wx.TextCtrl(self, name="Cal_1", value="", size=(240,-1))
self.Cal_1.SetDropTarget(file_drop_target)
grid.Add(self.Cal_1, pos=(3,1))
self.lblname = wx.StaticText(self, label="Cal File 2 :")
grid.Add(self.lblname, pos=(4,0))
self.Cal_2 = wx.TextCtrl(self, value="", name="Cal_2", size=(240,-1))
self.Cal_2.SetDropTarget(file_drop_target)
grid.Add(self.Cal_2, pos=(4,1))
self.lblname = wx.StaticText(self, label="Cal File 3 :")
grid.Add(self.lblname, pos=(5,0))
self.Cal_3 = wx.TextCtrl(self, value="", name="Cal_3", size=(240,-1))
self.Cal_3.SetDropTarget(file_drop_target)
grid.Add(self.Cal_3, pos=(5,1))
hSizer.Add(grid, 0, wx.ALL, 5)
mainSizer.Add(hSizer, 0, wx.ALL, 5)
mainSizer.Add(self.button, 0, wx.CENTER)
self.SetSizerAndFit(mainSizer)
self.Show(True)
def OnAbout(self,e):
# A message dialog box with an OK button. wx.OK is a standard ID in wxWidgets.
dlg = wx.MessageDialog( self, "A quick test to see if your scans pass repeatability", "DOMA-64 Tester", wx.OK)
dlg.ShowModal() # Show it
dlg.Destroy() # finally destroy it when finished.
def OnExit(self,e):
# Close the frame.
self.Close(True)
def SetInsertionPointEnd(self, y):
if y <= -31:
self.Cal_1.SetInsertionPointEnd()
elif y >= -1:
self.Cal_3.SetInsertionPointEnd()
else:
self.Cal_2.SetInsertionPointEnd()
def updateText(self, text, y):
if y <= -31:
self.Cal_1.WriteText(text)
elif y >= -1:
self.Cal_3.WriteText(text)
else:
self.Cal_2.WriteText(text)
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
The problem is that you only create one instance of the MyFileDropTarget class and then assign that same object to multiple widgets. It would appear that you need to create one drop target instance per widget. I refactored your code a bit, but here's one way to do it:
import wx
import os
#import pandas as pd
#import numpy as np
#import matplotlib.pyplot as ply
#from scipy.stats import linregress
class MyFileDropTarget(wx.FileDropTarget):
#----------------------------------------------------------------------
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
#----------------------------------------------------------------------
def OnDropFiles(self, x, y, filenames):
self.window.SetInsertionPointEnd(y)
#self.window.updateText("\n%d file(s) dropped at %d,%d:\n" %
# (len(filenames), x, y), y)
for filepath in filenames:
self.window.updateText(filepath + '\n', y)
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
file_drop_target = MyFileDropTarget(self)
file_drop_target2 = MyFileDropTarget(self)
file_drop_target3 = MyFileDropTarget(self)
#Create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
grid = wx.GridBagSizer(hgap=5, vgap=5)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
#Create a button
self.button = wx.Button(self, label="Test")
#self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
# Radio Boxes
sysList = ['QEXL','QEX10','QEX7']
wlList = ['1100', '1400', '1800']
sys = wx.RadioBox(self, label="What system are you calibrating ?",
pos=(20, 40), choices=sysList, majorDimension=3,
style=wx.RA_SPECIFY_COLS)
grid.Add(sys, pos=(1,0), span=(1,3))
WL = wx.RadioBox(self, label="Maximum WL you currently Calibrating ?",
pos=(20, 100), choices=wlList, majorDimension=0,
style=wx.RA_SPECIFY_COLS)
grid.Add(WL, pos=(2,0), span=(1,3))
self.lblname = wx.StaticText(self, label="Cal File 1 :")
grid.Add(self.lblname, pos=(3,0))
self.Cal_1 = wx.TextCtrl(self, name="Cal_1", value="", size=(240,-1))
self.Cal_1.SetDropTarget(file_drop_target)
grid.Add(self.Cal_1, pos=(3,1))
self.lblname = wx.StaticText(self, label="Cal File 2 :")
grid.Add(self.lblname, pos=(4,0))
self.Cal_2 = wx.TextCtrl(self, value="", name="Cal_2", size=(240,-1))
self.Cal_2.SetDropTarget(file_drop_target2)
grid.Add(self.Cal_2, pos=(4,1))
self.lblname = wx.StaticText(self, label="Cal File 3 :")
grid.Add(self.lblname, pos=(5,0))
self.Cal_3 = wx.TextCtrl(self, value="", name="Cal_3", size=(240,-1))
self.Cal_3.SetDropTarget(file_drop_target3)
grid.Add(self.Cal_3, pos=(5,1))
hSizer.Add(grid, 0, wx.ALL, 5)
mainSizer.Add(hSizer, 0, wx.ALL, 5)
mainSizer.Add(self.button, 0, wx.CENTER)
self.SetSizer(mainSizer)
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(400,400))
panel = MyPanel(self)
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Creating the menubar.
menubar = wx.MenuBar()
fileMenu = wx.Menu()
helpMenu = wx.Menu()
menubar.Append(fileMenu, '&File')
menuOpen = fileMenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
#self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
fileMenu.AppendSeparator()
menuExit = fileMenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
menubar.Append(helpMenu, '&Help')
menuAbout= helpMenu.Append(wx.ID_ABOUT, "&About",
" Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.SetMenuBar(menubar)
self.Show(True)
def OnAbout(self,e):
# A message dialog box with an OK button. wx.OK is a standard ID in wxWidgets.
dlg = wx.MessageDialog( self, "A quick test to see if your scans pass repeatability", "DOMA-64 Tester", wx.OK)
dlg.ShowModal() # Show it
dlg.Destroy() # finally destroy it when finished.
def OnExit(self,e):
# Close the frame.
#del self.fdt
self.Close(True)
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
UPDATE 2016.03.09 - I decided to post a refactored version of the OP's solution:
import wx
import os
#import pandas as pd
#import numpy as np
#import matplotlib.pyplot as ply
#from scipy.stats import linregress
class MyFileDropTarget(wx.FileDropTarget):
#----------------------------------------------------------------------
def __init__(self, obj):
wx.FileDropTarget.__init__(self)
self.obj = obj
#----------------------------------------------------------------------
def OnDropFiles(self, x, y, filenames):
#self.obj.SetInsertionPointEnd(y)
#self.obj.WriteText("\n%d file(s) dropped at %d,%d:\n" %
# (len(filenames), x, y))
for filepath in filenames:
self.obj.WriteText(filepath + '\n')
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
#Create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
grid = wx.GridBagSizer(hgap=5, vgap=5)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
#Create a button
self.button = wx.Button(self, label="Test")
#self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
# Radio Boxes
sysList = ['QEXL','QEX10','QEX7']
wlList = ['1100', '1400', '1800']
sys = wx.RadioBox(self, label="What system are you calibrating ?",
pos=(20, 40), choices=sysList, majorDimension=3,
style=wx.RA_SPECIFY_COLS)
grid.Add(sys, pos=(1,0), span=(1,3))
WL = wx.RadioBox(self, label="Maximum WL you currently Calibrating ?",
pos=(20, 100), choices=wlList, majorDimension=0,
style=wx.RA_SPECIFY_COLS)
grid.Add(WL, pos=(2,0), span=(1,3))
x = 3
for widget in range(1, 4):
lbl = wx.StaticText(self, label="Cal File {} :".format(widget))
grid.Add(lbl, pos=(x,0))
txt = wx.TextCtrl(self, name="Cal_{}".format(widget),
value="", size=(240,-1))
dt = MyFileDropTarget(txt)
txt.SetDropTarget(dt)
grid.Add(txt, pos=(x,1))
x += 1
hSizer.Add(grid, 0, wx.ALL, 5)
mainSizer.Add(hSizer, 0, wx.ALL, 5)
mainSizer.Add(self.button, 0, wx.CENTER)
self.SetSizer(mainSizer)
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(400,400))
panel = MyPanel(self)
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Creating the menubar.
menubar = wx.MenuBar()
fileMenu = wx.Menu()
helpMenu = wx.Menu()
menubar.Append(fileMenu, '&File')
menuOpen = fileMenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
#self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
fileMenu.AppendSeparator()
menuExit = fileMenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
menubar.Append(helpMenu, '&Help')
menuAbout= helpMenu.Append(wx.ID_ABOUT, "&About",
" Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.SetMenuBar(menubar)
self.Show(True)
def OnAbout(self,e):
# A message dialog box with an OK button. wx.OK is a standard ID in wxWidgets.
dlg = wx.MessageDialog( self, "A quick test to see if your scans pass repeatability", "DOMA-64 Tester", wx.OK)
dlg.ShowModal() # Show it
dlg.Destroy() # finally destroy it when finished.
def OnExit(self,e):
# Close the frame.
#del self.fdt
self.Close(True)
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
Note that you can use a loop to create those label and text widgets.
Mike, Your solution really helped me out thank you. Although it solved the problem with the window not closing it caused a problem with the ability to bring in the files and display their names. I modified the code you provided so that it will work for me and am posting it here in case anyone else has a problem similar to this. It is not the most elegant solution but it works. Thanks again.
import wx
import os
#import pandas as pd
#import numpy as np
#import matplotlib.pyplot as ply
#from scipy.stats import linregress
class MyFileDropTarget(wx.FileDropTarget):
#----------------------------------------------------------------------
def __init__(self, obj):
wx.FileDropTarget.__init__(self)
self.obj = obj
#----------------------------------------------------------------------
def OnDropFiles(self, x, y, filenames):
#self.obj.SetInsertionPointEnd(y)
#self.obj.WriteText("\n%d file(s) dropped at %d,%d:\n" %
# (len(filenames), x, y))
for filepath in filenames:
self.obj.WriteText(filepath + '\n')
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
#Create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
grid = wx.GridBagSizer(hgap=5, vgap=5)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
#Create a button
self.button = wx.Button(self, label="Test")
#self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
# Radio Boxes
sysList = ['QEXL','QEX10','QEX7']
wlList = ['1100', '1400', '1800']
sys = wx.RadioBox(self, label="What system are you calibrating ?",
pos=(20, 40), choices=sysList, majorDimension=3,
style=wx.RA_SPECIFY_COLS)
grid.Add(sys, pos=(1,0), span=(1,3))
WL = wx.RadioBox(self, label="Maximum WL you currently Calibrating ?",
pos=(20, 100), choices=wlList, majorDimension=0,
style=wx.RA_SPECIFY_COLS)
grid.Add(WL, pos=(2,0), span=(1,3))
self.lblname = wx.StaticText(self, label="Cal File 1 :")
grid.Add(self.lblname, pos=(3,0))
self.Cal_1 = wx.TextCtrl(self, name="Cal_1", value="", size=(240,-1))
Cal1 = MyFileDropTarget(self.Cal_1)
self.Cal_1.SetDropTarget(Cal1)
grid.Add(self.Cal_1, pos=(3,1))
self.lblname = wx.StaticText(self, label="Cal File 2 :")
grid.Add(self.lblname, pos=(4,0))
self.Cal_2 = wx.TextCtrl(self, value="", name="Cal_2", size=(240,-1))
Cal2 = MyFileDropTarget(self.Cal_2)
self.Cal_2.SetDropTarget(Cal2)
grid.Add(self.Cal_2, pos=(4,1))
self.lblname = wx.StaticText(self, label="Cal File 3 :")
grid.Add(self.lblname, pos=(5,0))
self.Cal_3 = wx.TextCtrl(self, value="", name="Cal_3", size=(240,-1))
Cal3 = MyFileDropTarget(self.Cal_3)
self.Cal_3.SetDropTarget(Cal3)
grid.Add(self.Cal_3, pos=(5,1))
hSizer.Add(grid, 0, wx.ALL, 5)
mainSizer.Add(hSizer, 0, wx.ALL, 5)
mainSizer.Add(self.button, 0, wx.CENTER)
self.SetSizer(mainSizer)
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(400,400))
panel = MyPanel(self)
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Creating the menubar.
menubar = wx.MenuBar()
fileMenu = wx.Menu()
helpMenu = wx.Menu()
menubar.Append(fileMenu, '&File')
menuOpen = fileMenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
#self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
fileMenu.AppendSeparator()
menuExit = fileMenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
menubar.Append(helpMenu, '&Help')
menuAbout= helpMenu.Append(wx.ID_ABOUT, "&About",
" Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.SetMenuBar(menubar)
self.Show(True)
def OnAbout(self,e):
# A message dialog box with an OK button. wx.OK is a standard ID in wxWidgets.
dlg = wx.MessageDialog( self, "A quick test to see if your scans pass repeatability", "DOMA-64 Tester", wx.OK)
dlg.ShowModal() # Show it
dlg.Destroy() # finally destroy it when finished.
def OnExit(self,e):
# Close the frame.
#del self.fdt
self.Close(True)
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()

How can wxThumbnailCtrl in wxpython be used?

I think this is a new widget and theres no examples online about this. im not sure how i should start using it or how it can be used.
Here is simple example to get you started. self.button selects directory with images; on doubleclick on any of thumbnails a messagebox is shown with info on selected thumb.
import wx
from wx.lib.agw import thumbnailctrl as tn
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, style=wx.DEFAULT_FRAME_STYLE)
self.button = wx.Button(self, -1, "Select dir")
self.Bind(wx.EVT_BUTTON, self.ButtonPress, self.button)
self.tn = tn.ThumbnailCtrl(self)
self.tn.Bind(tn.EVT_THUMBNAILS_DCLICK, self.TnClick)
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.tn, 1, wx.EXPAND, 0)
box.Add(self.button, 0, wx.ADJUST_MINSIZE, 0)
self.SetSizer(box)
box.Fit(self)
self.Layout()
def ButtonPress(self, evt):
dlg = wx.DirDialog(self, 'Get dir')
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
dlg.Destroy()
self.tn.ShowDir(path)
def TnClick(self, evt):
sel = self.tn.GetSelection()
wx.MessageBox(self.tn.GetThumbInfo(sel))
if __name__ == "__main__":
app = wx.PySimpleApp(0)
frame = MyFrame(None, -1, "")
frame.Show()
app.MainLoop()

Categories

Resources