wxPython's wx.grid.Grid() won't come fully into view - python

Issue:
I'm experiencing an issue where a function that simply creates a Grid() works when called in one place, but not another place. When it is called from the "other," non-working place, it does create a very small square in the corner of the window. At this time, I don't understand why, and I'm hoping someone can help.
Code: (feel free to copy paste this into a text editor and give it a go!)
import wx
import wx.grid as gridlib
class MainFrame(wx.Frame):
def __init__(self, parent, title):
super(MainFrame, self).__init__(parent, title=title,
size=(350, 250))
self.init_ux()
self.main_grid = None
def init_ux(self):
menu_bar = wx.MenuBar()
file_menu = wx.Menu()
file_menu.AppendSeparator()
menu_bar.Append(file_menu, '&File')
# add open file menu
open_menu = wx.Menu()
my_btn = open_menu.Append(wx.ID_ANY, 'button description')
# append open_menu to the file_menu
file_menu.Append(wx.ID_OPEN, '&Open', open_menu)
self.SetMenuBar(menu_bar)
self.Bind(wx.EVT_MENU, lambda event: self.open_dialog(data="i love string literals."), my_btn)
self.SetSize((300, 200))
self.Centre()
# the create_grid_view() below DOES work when uncommented
#self.create_grid_view(10, 10)
def create_grid_view(self, row_count, col_count):
print("Creating grid view!")
# set up grid panel
panel = wx.Panel(self)
self.main_grid = gridlib.Grid(panel)
self.main_grid.CreateGrid(row_count, col_count)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.main_grid, 1, wx.EXPAND)
panel.SetSizer(sizer)
def open_dialog(self, data):
# data is being used for populating wildcard, etc
with wx.FileDialog(self, "Open a file!",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return
file_path = fileDialog.GetPath()
try:
# here, I do some fun data "things" with the file_path
# open it, use other functions, etc.
# afterwards, I need to create a grid
self.create_grid_view(10, 10)
# ^^ This creates a small square instead of a grid.
except IOError:
wx.LogError("Cannot open file '%s'." % file_path)
if __name__ == "__main__":
app = wx.App()
frame = MainFrame(None, title='Window Title :)')
frame.Show()
app.MainLoop()
Expectation:
Actual Results:
Summary:
Why does the create_grid_view() function display a proper grid when called from the init_ux() function, but not the open_dialog() function?
Thanks in advance!

Where you have panel.SetSizer(sizer) either use:
panel.SetSizerAndFit(sizer)
or use:
panel.SetSizer(sizer)
panel.Fit()

Related

Go to a specific page on a pdf using wx.lib.pdfwin_old

It seems that wx.lib.pdfwin_old has functions for going to only the previous and next page for a pdf. How do you modify it to go to a specific page on a pdf or what's the code for going to a specific page on a pdf?
import wx
if wx.Platform == '__WXMSW__':
from wx.lib.pdfwin_old import PDFWindow
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=-1)
self.pdf = None
sizer = wx.BoxSizer(wx.VERTICAL)
self.pdf = PDFWindow(self, size=(parent.GetSize().width,parent.GetSize().height*3/5),style=wx.SUNKEN_BORDER)
sizer.Add(self.pdf, proportion=3, flag=wx.EXPAND|wx.ALL)
btn = wx.Button(self, wx.NewId(), "Open PDF File")
self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
sizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL)
self.SetSizer(sizer)
def OnOpenButton(self, event):
global current_pdf_path
dlg = wx.FileDialog(self, wildcard="*.pdf")
if dlg.ShowModal() == wx.ID_OK:
wx.BeginBusyCursor()
print(dlg.GetPath())
self.pdf.LoadFile(dlg.GetPath())
wx.EndBusyCursor()
dlg.Destroy()
app = wx.App()
frame = wx.Frame(None, -1, "PDFWindow", size = (640, 480))
MyPanel(frame)
frame.Show(True)
app.MainLoop()
Have you tried
setCurrentPage(n)
From pdfwin_old.py:
def setCurrentPage(self, npage):
"""
Goes to the specified page in the document. Maintains the
current location within the page and zoom level. npage is
the page number of the destination page. The first page
in a document is page 0.
## Oh no it isn't! The first page is 1 (dfh)
"""
if self.ie.Document:
return self.ie.Document.setCurrentPage(npage)
else:
raise PDFWindowError()
Edit:
Sorry Boikem, can't help! I didn't realise that pdfwin was windows only, although the clue is in the name. I run on Linux.
As a consolation, I offer this code below, a method using a browser, which may or may not give you some mileage. (python3)
import wx
import webbrowser
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=-1)
sizer = wx.BoxSizer(wx.VERTICAL)
btn = wx.Button(self, wx.NewId(), "Open PDF File")
self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
sizer.Add(btn, 0, flag=wx.EXPAND|wx.ALL)
self.SetSizer(sizer)
def OnOpenButton(self, event):
dlg = wx.FileDialog(self, wildcard="*.pdf")
if dlg.ShowModal() == wx.ID_OK:
url = dlg.GetPath()
dlg.Destroy()
try:
if not url:
return
except:
return
url = "file://"+url+'#page=12'
url = url.encode('UTF-8')
print(url)
browser_list = ("chromium-browser","firefox")
for x in browser_list:
try:
browser = webbrowser.get(x)
except:
pass
else:
browser.open(url)
break
try:
if browser:
pass
except:
print("Neither Supported browsers Firefox or Chromium found")
app = wx.App()
frame = wx.Frame(None, -1, "PDF Via Browser", size = (640, 480))
MyPanel(frame)
frame.Show(True)
app.MainLoop()
The chromium browser seems to be the most accurate.
Personally, for displaying help in my projects, I create a single main pdf and breakout of it, content specific pdf's for each topic by using sections within the main document. This method avoids issues when the page numbers change, as the documentation develops.
I'd suggest that you un-accept this answer, as it clearly does not answer your specific question.

wxPython - Setting a help string for a button in toolbar

I noticed I can have a help string appear in the status bar whenever I mouse over tools in my toolbar. I cannot find a way to accomplish this with text buttons.
My toolbar creation is similar to
# Make Tool Bar
toolbar = self.CreateToolBar()
# Make Tool Bar Items
# Play
self.addBasicTool(toolbar, "Play",
"This is my help string",
stuff.image_play,
self.OnPlay)
# My Button
btn = wx.Button(toolbar, wx.ID_OPEN, label="TEXT BUTTON ")
btn.Bind(wx.EVT_BUTTON, self.OnButtonPress)
toolbar.AddControl(btn)
addBasicTool just takes the image, scales it to a proper size, creates the tool with AddBasicTool, and binds the tool to the handler.
def addBasicTool(self, toolbar, label, desc, imgPath, handler):
icon_width=stuff.toolbar_icon_w
icon_height=stuff.toolbar_icon_h
size = (icon_width, icon_height)
img = wx.Image(imgPath, wx.BITMAP_TYPE_ANY).\
Scale(*size).ConvertToBitmap()
tool = toolbar.AddSimpleTool(-1, img, label, desc)
self.Bind(wx.EVT_MENU, handler, tool)
For the tool, the helper string is set pretty straight forward. I can't find anything to do the same with a button.
This button may just end up being a filler until I get an icon for it, but I'm still curious how helper strings can be done. I could have a handler that sets the statusBar when the mouse is over the button, but I feel like that is already done somewhere. Thanks the help
Basically you'll have to catch the mouse as it moves over your buttons and update the status bar accordingly. It's not very hard. You just need to bind to wx.EVT_MOTION. Here's a simple example:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.frame = parent
test_btn = wx.Button(self, label='Test Button')
test_btn.Bind(wx.EVT_MOTION, self.updateStatusBar)
test_btn_2 = wx.Button(self, label='Test Button')
test_btn_2.Bind(wx.EVT_MOTION, self.updateStatusBar)
self.buttons = {test_btn: 'Test help string',
test_btn_2: 'Another string'}
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(test_btn, 0, wx.ALL, 5)
main_sizer.Add(test_btn_2, 0, wx.ALL, 5)
self.SetSizer(main_sizer)
#----------------------------------------------------------------------
def updateStatusBar(self, event):
""""""
btn = event.GetEventObject()
if btn in self.buttons:
status = self.buttons[btn]
self.frame.sb.SetStatusText(status)
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title='Test Help Strings')
panel = MyPanel(self)
self.sb = self.CreateStatusBar()
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()

How to link multiple wx.Dialogs in wxPython

I want to make a game in wxPython (no other modules) and I want to make it so that you can enter some values in popup screens before the game starts, and then the game will be drawn on a canvas which in turn is drawn on a panel, which is bound to the main game.
I made the gamescreen with all fancy stuff (works solo)
I made the input screens
But I cannot link them.
How do I start the game so it will open a dialog box, then on the closure of it open another one, and then open the game ?
I tried the following, but it will not open my canvas:
# makes a game by showing 2 dialogs
# after dialogs have been answered, starts the game by drawing the canvas.
# imports
import wx
import Speelveld3
# globals
SCRWIDTH = 950
SCRHEIGHT = 700
# dialogbox class
class MyDialog1(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.username = wx.TextCtrl(self)
self.okButton = wx.Button(self, wx.ID_OK, "OK")
class MyDialog2(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.canvasWidth = wx.TextCtrl(self)
self.okButton = wx.Button(self, wx.ID_OK, "OK")
# main class
class Game(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, title='My game', size=(SCRWIDTH, SCRHEIGHT))
self.username = ""
self.canvasWidth = 10
# hide the frame for now
self.Hide()
def OnInit(self):
#Make your dialogs
dlg1 = MyDialog1(self)
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
if dlg1.ShowModal() == wx.ID_OK:
#get the username from the dialog
self.username = dlg1.username.GetValue()
#clean up the dialog (AFTER you get the username)
dlg1.Destroy()
dlg2 = MyDialog2(self)
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
if dlg2.ShowModal() == wx.ID_OK:
#get the username from the dialog
self.canvasWidth = dlg2.canvasWidth.GetValue()
#clean up the dialog (AFTER you get the username)
dlg2.Destroy()
# Now that you have your settings, Make the gameboard
# THIS PART IS STILL BROKEN!
# I can paste the whole board class (structure of it is taken from the tetris tutorial)
# but that seems a bit much tbh...
self.gameBoard = Board.Board(self)
self.gameBoard = SetFocus()
self.gameBoard.start()
self.Centre()
self.Show(True) #show the frame
if __name__ == '__main__':
# how can I start the game here?
app = wx.App()
frame = Game()
board = Speelveld3.Speelveld(frame)
board.start()
frame.Show()
app.MainLoop()
You've double posted, and the lack of any wx.Dialog in your sample code suggests to me that you haven't even looked at a tutorial yet, but I will give you the benefit of the doubt.
First, if you want to return information from a dialog, the easiest way is to define a custom dialog. Define a new class that inherits from wx.Dialog and then set it up just like you would a normal panel or a frame. It seems to me that you will need two of these. They'll look something like this:
class MyDialog1(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.username = wx.TextCtrl(self) #this is where users will enter their username
self.okButton = wx.Button(self, wx.ID_OK, "OK") #Note that I'm using wx.ID_OK. This is important
Now, for the logic you want. Pretty much every object in wxPython that you actually see has the functions Show() and Hide() (API here). You don't want to show your frame until AFTER the dialogs are finished, so in your __init__(), call Hide(). I'm also initializing a variable, username, which is where I will store the data from my dialog.
class Game(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(SCRWIDTH, SCRHEIGHT))
self.username = ""
self.Hide() #don't show the frame just yet
#self.Hide() is the exact same as self.Show(False)
Now, for your dialogs. Like Mike Driscoll suggested, you call your dialogs BEFORE making your canvas. wx.Dialogs are launched using ShowModal(). By setting the ID of self.okButton to the constant wx.ID_OK, wxPython recognizes that the dialog should be closed after the button in clicked. You should also be aware of wx.ID_CANCEL.
def OnInit(self):
#Make your dialogs
dlg1 = MyDialog1(self)
if dlg1.ShowModal() == wx.ID_OK:
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
self.username = dlg1.username.GetValue() #get the username from the dialog
dlg1.Destroy() #clean up the dialog (AFTER you get the username)
#do this again for your second dialog
#Now that you have your settings, Make the gameboard
self.gameBoard = Board.Board(self)
self.gameBoard = SetFocus()
self.gameBoard.start()
self.Centre()
self.Show(True) #show the frame
In your OnInit you just need to call your dialogs and show them modally BEFORE you create your Board instance. Then it should work correctly.
EDIT (6-28-12): Here's some code:
import wx
########################################################################
class MyDlg(wx.Dialog):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Dialog.__init__(self, None, title="I'm a dialog!")
lbl = wx.StaticText(self, label="Hi from the panel's init!")
btn = wx.Button(self, id=wx.ID_OK, label="Close me")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(lbl, 0, wx.ALL, 5)
sizer.Add(btn, 0, wx.ALL, 5)
self.SetSizer(sizer)
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
# show a custom dialog
dlg = MyDlg()
dlg.ShowModal()
dlg.Destroy()
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
pdc = wx.PaintDC(self)
try:
dc = wx.GCDC(pdc)
except:
dc = pdc
rect = wx.Rect(0,0, 100, 100)
for RGB, pos in [((178, 34, 34), ( 50, 90)),
(( 35, 142, 35), (110, 150)),
(( 0, 0, 139), (170, 90))
]:
r, g, b = RGB
penclr = wx.Colour(r, g, b, wx.ALPHA_OPAQUE)
brushclr = wx.Colour(r, g, b, 128) # half transparent
dc.SetPen(wx.Pen(penclr))
dc.SetBrush(wx.Brush(brushclr))
rect.SetPosition(pos)
dc.DrawRoundedRectangleRect(rect, 8)
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Example frame")
# show a MessageDialog
style = wx.OK|wx.ICON_INFORMATION
dlg = wx.MessageDialog(parent=None,
message="Hello from the frame's init",
caption="Information", style=style)
dlg.ShowModal()
dlg.Destroy()
# create panel
panel = MyPanel(self)
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
frame.Show()
app.MainLoop()

Creating ScrolledWindow in wxPython

I am trying to make a ScrolledWindow that can scroll over a grid of images, but the scrollbar isn't appearing. wxWidgets documentation says:
The most automatic and newest way [to set the scrollbars in wxScrolledWindow] is to simply let sizers determine the scrolling area. This is now the default when you set an interior sizer into a wxScrolledWindow with wxWindow::SetSizer. The scrolling area will be set to the size requested by the sizer and the scrollbars will be assigned for each orientation according to the need for them and the scrolling increment set by wxScrolledWindow::SetScrollRate
So I try to set the sizer of my ScrolledWindow with a GridSizer but it's not working. The code:
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id=-1,title="",pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE,
name="frame"):
wx.Frame.__init__(self,parent,id,title,pos,size,style,name)
self.panel = wx.ScrolledWindow(self,wx.ID_ANY)
menuBar = wx.MenuBar()
menu1 = wx.Menu()
m = menu1.Append(wx.NewId(), "&Blah", "Show Pictures")
menuBar.Append(menu1,"&Blah")
self.Bind(wx.EVT_MENU,self.OnInit,m)
self.SetMenuBar(menuBar)
def OnInit(self, event):
sizer = wx.GridSizer(rows=7,cols=3)
filenames = []
for i in range(20):
filenames.append("img"+str(i)+".png")
for fn in filenames:
img = wx.Image(fn,wx.BITMAP_TYPE_ANY)
sizer.Add(wx.StaticBitmap(self.panel,wx.ID_ANY,wx.BitmapFromImage(img)))
self.panel.SetSizer(sizer)
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(parent=None,title="Frame")
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
Insert this
self.panel.SetScrollbars(1, 1, 1, 1)
after self.panel = wx.ScrolledWindow(self,wx.ID_ANY)
If you want some info on the SetScrollBars method then look at this wxwidgets documentation page

wxPython: Good way to overlay a wx.Panel on an existing wx.Panel

I have a wx.Frame, in which there is a main wx.Panel with several widgets inside of it. I want one button in there to cause a "help panel" to come up. This help panel would probably be a wx.Panel, and I want it to overlay the entire main wx.Panel (not including the menu bar of the wx.Frame). There should be some sort of close button on the help button that will make it disappear again.
What is a good way to achieve this? I've looked into wx.Notebook but haven't found a way to make it not show the tabs.
Note that I don't want to destroy and recreate the help panel every time the user closes and opens it: I just want it to be hidden.
There are several ways
a) you can create a custom child panel, and make it same size and position at 0,0 among top of all child widgets. no need of destroying it just Show/Hide it
this also resizes with parent frame
b) popup a wx.PopupWindow or derived class and place and size it at correct location
so as suggest in a) here is an example, where all controls are put in panel using sizer, as separate help cntrl is created which can be shown/hidden from button, but you can create a custom cntrl which hides itself on clicking close
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.panel = wx.Panel(self)
# create controls
self.cntrlPanel = wx.Panel(self.panel)
stc1 = wx.StaticText(self.cntrlPanel, label="wow it works")
stc2 = wx.StaticText(self.cntrlPanel, label="yes it works")
btn = wx.Button(self.cntrlPanel, label="help?")
btn.Bind(wx.EVT_BUTTON, self._onShowHelp)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(stc1)
sizer.Add(stc2)
sizer.Add(btn)
self.cntrlPanel.SetSizer(sizer)
# create help panel
self.helpPanel = wx.Panel(self.panel)
self.stcHelp = wx.StaticText(self.helpPanel, label="help help help\n"*8)
btn = wx.Button(self.helpPanel, label="close[x]")
btn.Bind(wx.EVT_BUTTON, self._onShowCntrls)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.stcHelp)
sizer.Add(btn)
self.helpPanel.SetSizer(sizer)
self.helpPanel.Hide()
self.helpPanel.Raise()
self.helpPanel.SetBackgroundColour((240,250,240))
self.Bind(wx.EVT_SIZE, self._onSize)
self._onShowCntrls(None)
def _onShowHelp(self, event):
self.helpPanel.SetPosition((0,0))
self.helpPanel.Show()
self.cntrlPanel.Hide()
def _onShowCntrls(self, event):
self.cntrlPanel.SetPosition((0,0))
self.helpPanel.Hide()
self.cntrlPanel.Show()
def _onSize(self, event):
event.Skip()
self.helpPanel.SetSize(self.GetClientSizeTuple())
self.cntrlPanel.SetSize(self.GetClientSizeTuple())
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.SetTopWindow(frame)
app.MainLoop()
Updated code for Python 3.9.4 and wxPython 4.1.1. Hope people find it useful. The original post was useful to me, but as a neophyte, it took me some effort to have it run properly with recently released tools.
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.panel = wx.Panel(self)
# create controls
self.cntrlPanel = wx.Panel(self.panel)
stc1 = wx.StaticText(self.cntrlPanel, label="wow it works")
stc2 = wx.StaticText(self.cntrlPanel, label="yes it works")
btn = wx.Button(self.cntrlPanel, label="help?")
btn.Bind(wx.EVT_BUTTON, self._onShowHelp)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(stc1)
sizer.Add(stc2)
sizer.Add(btn)
self.cntrlPanel.SetSizer(sizer)
# create help panel
self.helpPanel = wx.Panel(self.panel)
self.stcHelp = wx.StaticText(self.helpPanel, label="help help help\n"*8)
btn = wx.Button(self.helpPanel, label="close[x]")
btn.Bind(wx.EVT_BUTTON, self._onShowCntrls)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.stcHelp)
sizer.Add(btn)
self.helpPanel.SetSizer(sizer)
self.helpPanel.Hide()
self.helpPanel.Raise()
self.helpPanel.SetBackgroundColour((240,250,240))
self.Bind(wx.EVT_SIZE, self._onSize)
self._onShowCntrls(None)
def _onShowHelp(self, event):
self.helpPanel.SetPosition((0,0))
self.helpPanel.Show()
self.cntrlPanel.Hide()
def _onShowCntrls(self, event):
self.cntrlPanel.SetPosition((0,0))
self.helpPanel.Hide()
self.cntrlPanel.Show()
def _onSize(self, event):
event.Skip()
self.helpPanel.SetSize(self.GetClientSize())
self.cntrlPanel.SetSize(self.GetClientSize())
app = wx.App(False)
frame = MyFrame()
frame.Show()
app.SetTopWindow(frame)
app.MainLoop()

Categories