I have written a small bit of code in wxpython in which the splitter window is used to split the dialog box and one panel has custom tree ctrl and the other has static box.
This code was working fine in Fedora and ubuntu, but in Windows the dialog box is appearing empty.
Main code:
wx.Dialog.__init__(self, None, 6, 'Filters',size = (500,600) )
self.splitter = wx.SplitterWindow(self, -1)
leftPanel = wx.Panel(self.splitter, -1)
leftBox = wx.BoxSizer(wx.VERTICAL)
self.__tree = TreeFilter(leftPanel, 1)
leftBox.Add(self.__tree, -1, wx.GROW)
self.__tree.Bind(CT.EVT_TREE_ITEM_CHECKED, self.checked)
leftPanel.SetSizer(leftBox)
rightPanel = wx.Panel(self.splitter, -1)
rightBox = wx.BoxSizer(wx.VERTICAL)
self.display = wx.StaticText(rightPanel, -1, '', style=wx.ALIGN_LEFT)
rightBox.Add(self.display, -1, wx.GROW)
btnsizer = wx.StdDialogButtonSizer()
btn = wx.Button(rightPanel, wx.ID_OK)
btn.SetDefault()
btnsizer.AddButton(btn)
btn = wx.Button(rightPanel, wx.ID_CANCEL)
btnsizer.AddButton(btn)
btnsizer.Realize()
rightBox.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
rightPanel.SetSizer(rightBox)
self.splitter.SplitVertically(leftPanel, rightPanel)
self.Centre()
where TreeFilter is a custom tree ctrl.
Please help me fixing the problem
The problem is that you don't have the splitter itself in a sizer that's set to expand. On my screen, you can see a little box all scrunched up in the left hand corner, so I knew something like that was happening.
Here's an example that works for me:
import wx
########################################################################
class MyTreeCtrl(wx.TreeCtrl):
#----------------------------------------------------------------------
def __init__(self, parent, id):
wx.TreeCtrl.__init__(self, parent, id)
########################################################################
class TestDlg(wx.Dialog):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Dialog.__init__(self, None, 6, 'Filters',size = (500,600) )
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.splitter = wx.SplitterWindow(self, -1)
leftPanel = wx.Panel(self.splitter, -1)
leftBox = wx.BoxSizer(wx.VERTICAL)
self.tree = MyTreeCtrl(leftPanel, 1)
self.root = self.tree.AddRoot("The Root Item")
self.tree.SetPyData(self.root, None)
for x in range(15):
child = self.tree.AppendItem(self.root, "Item %d" % x)
self.tree.SetPyData(child, None)
self.tree.Expand(self.root)
leftBox.Add(self.tree, -1, wx.GROW)
#self.__tree.Bind(CT.EVT_TREE_ITEM_CHECKED, self.checked)
leftPanel.SetSizer(leftBox)
rightPanel = wx.Panel(self.splitter, -1)
rightBox = wx.BoxSizer(wx.VERTICAL)
self.display = wx.StaticText(rightPanel, -1, '', style=wx.ALIGN_LEFT)
rightBox.Add(self.display, -1, wx.GROW)
btnsizer = wx.StdDialogButtonSizer()
btn = wx.Button(rightPanel, wx.ID_OK)
btn.SetDefault()
btnsizer.AddButton(btn)
btn = wx.Button(rightPanel, wx.ID_CANCEL)
btnsizer.AddButton(btn)
btnsizer.Realize()
rightBox.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
rightPanel.SetSizer(rightBox)
self.splitter.SplitVertically(leftPanel, rightPanel)
self.Centre()
mainSizer.Add(self.splitter, 1, wx.EXPAND)
self.SetSizer(mainSizer)
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
dlg = TestDlg()
dlg.Show()
dlg.Destroy()
app.MainLoop()
Related
i've made this code :
import wx
import List as li
from ChoiceBook import *
class MainTab(wx.Panel):
def __init__(self, parent, sb, dm):
wx.Panel.__init__(self, parent)
self.parent = parent
self.hardware = []
box = wx.StaticBox(self, wx.ID_ANY, "Appareil")
self.list = li.List(self, sb, dm)
sizerleft = wx.StaticBoxSizer(box,wx.HORIZONTAL)
sizerleft.Add(self.list, 1, wx.ALL|wx.EXPAND, 5)
box = wx.StaticBox(self, wx.ID_ANY, "Mesures")
self.notebook = Choicebook(self,dm)
sizerright = wx.StaticBoxSizer(box,wx.HORIZONTAL)
sizerright.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(sizerleft, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(sizerright, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
def update(self):
self.notebook.update()
it made this window, but when i resize it, it dosen't scale the contents. it just crop it, like this.
I think the probleme come from the StaticBox which are static i guess(i'm not sure).
I would like to know if it's possible to make it resize automatically?
or if not is there is another way to make a box whith a title like StaticBox and so make it resizable ?
EDIT: i figure it out with your help. in fact this wasn't this portion of cod fault but my list Class. i just remove all size in that file
before
wx.ListCtrl.__init__(self,
parent,
size=(800, -1),
style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
self.rm = RM.Res_Man()
self.index = 0
self.InsertColumn(0, 'Appareil de mesure', width=125)
self.InsertColumn(1, 'Connecté')
self.InsertColumn(2, 'ID',width=300)
self.InsertColumn(3, 'Type',width=290)
self.sb = sb
after
wx.ListCtrl.__init__(self,
parent,
style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
self.rm = RM.Res_Man()
self.index = 0
self.InsertColumn(0, 'Appareil de mesure')
self.InsertColumn(1, 'Connecté')
self.InsertColumn(2, 'ID')
self.InsertColumn(3, 'Type')
self.sb = sb
thanks you all ;)
I think that what you want, is to force it to Layout() on a RESIZE event.
However, remember that these items will have a minimum size.
Try this:
import wx
class MainTab(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.hardware = []
box = wx.StaticBox(self, wx.ID_ANY, "Appareil")
lc = wx.ListCtrl(self, wx.ID_ANY, style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_VRULES)
lc.InsertColumn(1, "Appareil de mesure")
lc.InsertColumn(2, "Connecte")
lc.InsertColumn(3, "ID")
lc.InsertColumn(4, "Type")
sizerleft = wx.StaticBoxSizer(box,wx.HORIZONTAL)
sizerleft.Add(lc, 1, wx.ALL|wx.EXPAND, 5)
box = wx.StaticBox(self, wx.ID_ANY, "Mesures")
cb = wx.Choicebook(self, wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, name="")
sizerright = wx.StaticBoxSizer(box,wx.HORIZONTAL)
sizerright.Add(cb, 1, wx.ALL|wx.EXPAND, 5)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(sizerleft, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(sizerright, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
self.Bind(wx.EVT_SIZE, self.ReSize)
def ReSize(self,event):
self.Layout()
if __name__ == '__main__':
app = wx.App()
frame = MainTab()
frame.Show(True)
app.MainLoop()
I have had to hack it together as your code is in complete.
I've not been able to find an answer in the documentation. Say I have the following:
import wx
from wx import *
import sys
app = wx.App()
def quitProgram(*args):
sys.exit()
def restart(*args):
app.MainLoop()
xSize = 500
ySize = 300
window = wx.Frame(None, title = "My GUI", size = (xSize,ySize))
panel = wx.Panel(window)
# generic label
labelLeft = wx.StaticText(panel, label = 'some text', pos = (2,30))
# exit button
exit = wx.Button(panel, -1, label="Exit", pos = (1, 1), size=(-1,-1))
exit.Bind(wx.EVT_BUTTON, quitProgram)
# reset button
reset = wx.Button(panel, -1, label="Refresh", pos = (100,1), size=(-1,-1))
reset.Bind(wx.EVT_BUTTON, restart)
window.Show(True)
app.MainLoop()
How can I position the objects- buttons and labels- based on the size of the main window? I'd like the objects to reposition based on resizing the window.
The code posted is not clean wxPython code.
You must instantiate your Frame as a Class and use Sizers to automate positioning of your widgets.
A minimal code that reproduces your frame using Sizers is this:
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((500, 300))
self.bt_exit = wx.Button(self, wx.ID_ANY, "exit")
self.bt_refresh = wx.Button(self, wx.ID_ANY, "refresh")
self.text_ctrl = wx.TextCtrl(self, wx.ID_ANY, "some text", style=wx.TE_MULTILINE)
self.SetTitle("My GUI")
self.bt_exit.Bind(wx.EVT_BUTTON, self.on_exit)
self.bt_refresh.Bind(wx.EVT_BUTTON, self.on_refresh)
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2.Add(self.bt_exit, 1, 0, 0)
sizer_2.Add(self.bt_refresh, 1, 0, 0)
sizer_1.Add(sizer_2, 0, wx.EXPAND, 0)
sizer_1.Add(self.text_ctrl, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
self.Layout()
def on_exit(self, evt):
self.Close()
def on_refresh(self, evt):
self.text_ctrl.Clear()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
This is well explained in wxPython/Phoenyx docs. Check for example these tutorials
I have a problem: I want to make a password manager for myself .
I have the logic for the en/decoding done and also I have the dynamic adding of passwords done, but if I add too many passwords to my manager then I can't see all of the passwords because my screen is too small.
I wanted to add a scroll bar but the scroll bar didn't work.
Even after several hours of researching the code didn't want to work. This is the code for the PanelTwo:
class PanelTwo(wx.Panel):
#staticmethod
def scale_bitmap(bitmap, width, height):
image = wx.ImageFromBitmap(bitmap)
image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
result = wx.BitmapFromImage(image)
return result
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.refr = wx.Button(self,wx.ID_ANY,u"Refresh")
self.refr.Bind(wx.EVT_BUTTON, self.refresh)
self.co = wx.Button(self, wx.ID_ANY, u"Close")
self.co.Bind(wx.EVT_BUTTON, self.close)
self.number_of_pwds = 0
self.frame = parent
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
controlSizer = wx.BoxSizer(wx.HORIZONTAL)
self.widgetSizer = wx.BoxSizer(wx.VERTICAL)
#controlSizer.Add(self.Passest, 0, wx.CENTER | wx.ALL, 5)
controlSizer.Add(self.refr, 0, wx.CENTER | wx.ALL, 5)
controlSizer.Add(self.co, 0, wx.CENTER | wx.ALL, 5)
self.addButton = wx.Button(self, label="Add")
self.addButton.Bind(wx.EVT_BUTTON, self.onAddWidget)
controlSizer.Add(self.addButton, 0, wx.CENTER | wx.ALL, 5)
self.removeButton = wx.Button(self, label="Remove")
self.removeButton.Bind(wx.EVT_BUTTON, self.onRemoveWidget)
controlSizer.Add(self.removeButton, 0, wx.CENTER | wx.ALL, 5)
self.mainSizer.Add(controlSizer, 0, wx.CENTER)
self.mainSizer.Add(self.widgetSizer, 0, wx.CENTER | wx.ALL, 10)
self.SetSizer(self.mainSizer)
The adding and removing of the Pwds is here :
def onAddWidget(self, event):
self.number_of_pwds += 1
label = "Pwd %s" % self.number_of_pwds
name = "Pwd%s" % self.number_of_pwds
new_Text = wx.StaticText(self, label=label, name=name)
self.widgetSizer.Add(new_Text, 0, wx.ALL, 5)
self.frame.fSizer.Layout()
self.frame.Fit()
def onRemoveWidget(self, event):
if self.widgetSizer.GetChildren():
self.widgetSizer.Hide(self.number_of_pwds - 1)
self.widgetSizer.Remove(self.number_of_pwds - 1)
self.number_of_pwds -= 1
self.frame.fSizer.Layout()
self.frame.Fit()
my main Form is here :
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Passwort Manager",
size=(300,130))
self.panel_one = PanelOne(self)
self.panel_two = PanelTwo(self)
self.panel_three = PanelThree(self)
self.panel_two.Hide()
self.panel_three.Hide()
self.fSizer = wx.BoxSizer(wx.VERTICAL)
self.fSizer.Add(self.panel_one, 1, wx.EXPAND)
self.fSizer.Add(self.panel_two, 1, wx.EXPAND)
self.fSizer.Add(self.panel_three,1,wx.EXPAND)
self.SetSizer(self.fSizer)
self.SetBackgroundColour(Color.White)
def onSwitchPanels(self, event):
if self.panel_one.IsShown():
self.SetTitle("Passwort Manager")
self.SetBackgroundColour(Color.Random())
self.panel_one.Hide()
self.panel_two.Show()
elif self.panel_two.IsShown():
self.SetTitle("Passwort Manager")
self.SetBackgroundColour(Color.Random())
self.panel_three.Show()
self.panel_two.Hide()
else:
self.SetTitle("Passwort Manager")
self.SetBackgroundColour(Color.Random())
self.panel_one.Show()
self.panel_three.Hide()
self.Layout()
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
So how can I add a dynamic scrollbar that resizes automatically and that works with my code? I am using Python3.6.
Thanks for helping me out.
Have you tried using a scrolled panel?
import wx
import wx.lib.scrolledpanel as scrolled
class TestPanel(scrolled.ScrolledPanel):
def __init__(self, parent):
scrolled.ScrolledPanel.__init__(self, parent, -1)
"""
Write your code here...
"""
self.SetupScrolling()
When I only hade one TxtCtrl, it's right side would expand all the way to the windows edge. But after I created a vertical BoxSizer to and added the horizontal one to that, the TxtCtrol only had a width of about 100 pixels. Why is this?
import wx
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, style=wx.SIMPLE_BORDER)
top_box = wx.BoxSizer(wx.VERTICAL)
box1 = wx.BoxSizer(wx.HORIZONTAL)
box2 = wx.BoxSizer(wx.HORIZONTAL)
textureName = wx.TextCtrl(self, 1)
texturePath = wx.TextCtrl(self, 1)
box1.Add(wx.StaticText(self, 1, "Name: "), 0, wx.LEFT|wx.RIGHT|wx.TOP, 5)
box1.Add(textureName, 1, wx.ALIGN_LEFT|wx.RIGHT|wx.TOP, 5)
box2.Add(wx.StaticText(self, 1, "Path: "), 0, wx.LEFT|wx.RIGHT|wx.TOP, 5)
box2.Add(texturePath, 1, wx.ALIGN_LEFT|wx.RIGHT|wx.TOP, 5)
top_box.Add(box1)
top_box.Add(box2)
self.SetSizer(top_box)
class MainFrame(wx.Frame):
def __init__(self, parent, id):
title = "Exporter"
wx.Frame.__init__(self, parent, wx.ID_ANY, title,
size=(500, 420),
style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
self.panel = MainPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MainFrame(None, -1)
frame.Centre()
app.MainLoop()
pass
You need to use wx.EXPAND in the style.
top_box.Add(box1, 0, wx.EXPAND)
top_box.Add(box2)
From here: "When an item is Add'ed with the wxEXPAND flag, the item will be resized to fill its alloted area in the opposite orientation."
I'm having difficulty getting my sizers to work properly in wxpython. I am trying to do a simple one horizontal bar at top (with text in it) and two vertical boxes below (with gridsizers * the left one should only be 2 columns!! * inside each). I want the everything in the image to stretch and fit my panel as well (with the ability to add padding to sides and top/bottom).
I have two main issues:
1. I cant get the text in the horizontal bar to be in the middle (it goes to the left)
2. I would like to space the two vertical boxes to span AND fit the page appropriately (also would like the grids to span better too).
Here is my code (with some parts omitted):
self.LeagueInfoU = wx.Panel(self.LeagueInfo,-1, style=wx.BORDER_NONE)
self.LeagueInfoL = wx.Panel(self.LeagueInfo,-1, style=wx.BORDER_NONE)
self.LeagueInfoR = wx.Panel(self.LeagueInfo,-1, style=wx.BORDER_NONE)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
vbox2a = wx.GridSizer(12,2,0,0)
vbox3a = wx.GridSizer(10,3,0,0)
hbox1a = wx.BoxSizer(wx.VERTICAL)
vbox2 = wx.BoxSizer(wx.VERTICAL)
vbox3 = wx.BoxSizer(wx.VERTICAL)
hbox1.Add(self.LeagueInfoU, 1, wx.EXPAND | wx.ALL, 3)
vbox2.Add(self.LeagueInfoL, 1, wx.EXPAND | wx.ALL, 3)
vbox3.Add(self.LeagueInfoR, 1, wx.EXPAND | wx.ALL, 3)
vbox2a.AddMany([this is all correct])
self.LeagueInfoL.SetSizer(vbox2a)
vbox3a.AddMany([this is all correct])
self.LeagueInfoR.SetSizer(vbox3a)
font = wx.Font(20, wx.DEFAULT, wx.NORMAL, wx.BOLD)
self.Big_Header = wx.StaticText(self.LeagueInfoU, -1, 'Testing This')
self.Big_Header.SetFont(font)
hbox1a.Add(self.Big_Header, 0, wx.ALIGN_CENTER|wx.ALIGN_CENTER_VERTICAL)
self.LeagueInfoU.SetSizer(hbox1a)
hbox2.Add(vbox2, 0, wx.EXPAND)
hbox2.Add(vbox3, 0, wx.EXPAND)
vbox.Add(hbox1, 0, wx.EXPAND)
vbox.Add(hbox2, 1, wx.EXPAND)
self.LeagueInfo.SetSizer(vbox)
Is this what you're after?
import wx
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
main_sizer = wx.BoxSizer(wx.VERTICAL)
# Title
self.centred_text = wx.StaticText(self.panel, label="Title")
main_sizer.Add(self.centred_text, 0, wx.ALIGN_CENTRE | wx.ALL, 3)
# Grids
content_sizer = wx.BoxSizer(wx.HORIZONTAL)
grid_1 = wx.GridSizer(12, 2, 0, 0)
grid_1.AddMany(wx.StaticText(self.panel, label=str(i)) for i in xrange(24))
content_sizer.Add(grid_1, 1, wx.EXPAND | wx.ALL, 3)
grid_2 = wx.GridSizer(10, 3, 0, 0)
grid_2.AddMany(wx.StaticText(self.panel, label=str(i)) for i in xrange(30))
content_sizer.Add(grid_2, 1, wx.EXPAND | wx.ALL, 3)
main_sizer.Add(content_sizer, 1, wx.EXPAND)
self.panel.SetSizer(main_sizer)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
Frame(None)
app.MainLoop()
something like this??
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,"Test Stretching!!")
p1 = wx.Panel(self,-1,size=(500,100))
p1.SetMinSize((500,100))
p1.SetBackgroundColour(wx.GREEN)
hsz = wx.BoxSizer(wx.HORIZONTAL)
p2 = wx.Panel(self,-1,size=(200,400))
p2.SetMinSize((200,400))
p2.SetBackgroundColour(wx.RED)
p3 = wx.Panel(self,-1,size=(300,400))
p3.SetMinSize((300,400))
p3.SetBackgroundColour(wx.BLUE)
hsz.Add(p2,1,wx.EXPAND)
hsz.Add(p3,1,wx.EXPAND)
sz = wx.BoxSizer(wx.VERTICAL)
sz.Add(p1,0,wx.EXPAND)
sz.Add(hsz,1,wx.EXPAND)
self.SetSizer(sz)
self.Layout()
self.Fit()
a = wx.App(redirect=False)
f = MyFrame()
f.Show()
a.MainLoop()
Here's one way to do it:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
mainSizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
leftGridSizer = wx.GridSizer(rows=10, cols=12, vgap=5, hgap=5)
rightGridSizer = wx.GridSizer(rows=10, cols=3, vgap=5, hgap=5)
title = wx.StaticText(self, label="Main title")
mainSizer.Add(wx.StaticText(self), 0, wx.EXPAND) # add a "spacer"
mainSizer.Add(title, 0, wx.CENTER, wx.ALL, 10)
for row in range(1, 11):
for col in range(1, 13):
lbl = "Row%s Col%s" % (row, col)
leftGridSizer.Add(wx.StaticText(self, label=lbl))
hSizer.Add(leftGridSizer, 0, wx.ALL, 20)
for row in range(1, 11):
for col in range(1, 4):
lbl = "Row%s Col%s" % (row, col)
rightGridSizer.Add(wx.StaticText(self, label=lbl))
hSizer.Add(rightGridSizer, 0, wx.ALL, 20)
mainSizer.Add(hSizer)
self.SetSizer(mainSizer)
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Sizers", size=(1600,600))
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
To learn about spanning rows, I recommend looking at the wxPython demo. I think that may only be supported in wx.GridBagSizer and the FlexGridSizer though. You can try the span parameter though. Also, it should be noted that wx.GROW and wx.EXPAND are one and the same. You might also want to check out the wiki for more information: http://wiki.wxpython.org/GridBagSizerTutorial