wxpython wxgrid question

R

rbann11

Hi,

I am looking for example code that consists of just a frame and a
grid(10x2). The grid must fill the its parent even if the frame is
resized.

Thanks in advance,

Roger
 
J

Jim Segrave

Hi,

I am looking for example code that consists of just a frame and a
grid(10x2). The grid must fill the its parent even if the frame is
resized.

This simple program makes a two element window, the lower half of
which is a gridded set of labels which resize with the window. The
important part is the columnconfigure() and rowconfigure() method
calls on the container widget for the grid.
See
<URL:http://infohost.nmt.edu/tcc/help/pubs/tkinter/grid-config.html>
http://infohost.nmt.edu/tcc/help/pubs/tkinter/grid-config.html

#!/usr/local/bin/python

from Tkinter import *

root = Tk()
# create a frame (unused, but shared with gridded frame)
f = Frame(height = 100)
f.pack(expand = YES, fill = BOTH)

# create a frame for a gridded display
gf = Frame()
gf.pack(expand = YES, fill = BOTH)

# create a 3 x 4 array of labels, each with a different background
# Make each row and column have equal weights, so they'll
# grow and shrink together
for row in range(3):
gf.rowconfigure(row, weight = 1)
for col in range(4):
gf.columnconfigure(col, weight = 1)
Label(gf, text = "Row: %d\nCol: %d" % (row, col),
bg = "#%02x%02x%02x" % ((row * 4 + col) * 16,
(row * 4 + col) * 16,
(row * 4 + col) * 16),
fg = "#ffffff"
).grid(row = row, column = col, sticky = NSEW)



root.mainloop()
 
T

Tim Roberts

I am looking for example code that consists of just a frame and a
grid(10x2). The grid must fill the its parent even if the frame is
resized.

Have you gone through the wxPython demo application? It contains examples
of every standard control and almost every behavior you might want.
 
R

rbann11

Tim said:
Have you gone through the wxPython demo application? It contains examples
of every standard control and almost every behavior you might want.

Yes, and i have even checked out "wxpython in action". All of the
examples tend to leave white space on the right of the frame. I tried
basic a example with sizers and it didnt work. That why I was
wondering if someone had got it to work.


Roger
 
J

jean-michel bain-cornu

(e-mail address removed) a écrit :
Yes, and i have even checked out "wxpython in action". All of the
examples tend to leave white space on the right of the frame. I tried
basic a example with sizers and it didnt work. That why I was
wondering if someone had got it to work.


Roger
Hi Roger,
A key point is that the Grid manages itself its available space
according to the size it can have.
If you just tried to create a simple grid within a simple frame, you
probably got a grid filling all the frame space, and it's what you want.
Why that ?
If you do it (for instance with the script below), and you try to
manually reduce/increase the size of the window, you should see
scrollbars at the edge of the window ; these scrollbars come from the
grid, because they take in account the labels row and col (wxGrid comes
from wxScrolledWindow). You can see that no more space is available
beyond the scrollbar, so the grid takes the whole space.
And why the white space on the right ? This space is not on the right of
the grid, but on the right of the last col. We could think it's like
that because it's not possible to compute an appropriate col size for
the grid cols, but it's not displayed exactly the same in linux and in
windows. In one case it's over the last col (not enough space) and in
the other case it's beyond (too much space). I think that as the program
must work on all the environments, the interface must stay enough
global, and sometimes the display is not perfectly done.
The advantage is that if we let wx decide, we won't have to think how to
set the widgets.
Regards,
jm

#----------------------------------------------------------------------
import wx,wx.grid
#----------------------------------------------------------------------
class MainWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,parent,wx.ID_ANY,title)
#--
self.grid= wx.grid.Grid(id=wx.ID_ANY,parent=self)
self.grid.CreateGrid(numRows=10,numCols=2)
self.grid.Fit()
self.Fit()
#--
self.Show(1)
#----------------------------------------------------------------------
app = wx.PySimpleApp()
frame=MainWindow(None,-1,'Grid sizer')
app.MainLoop()
del app
#----------------------------------------------------------------------
 
R

rbann11

jean-michel bain-cornu said:
(e-mail address removed) a écrit :
Hi Roger,
A key point is that the Grid manages itself its available space
according to the size it can have.
If you just tried to create a simple grid within a simple frame, you
probably got a grid filling all the frame space, and it's what you want.
Why that ?
If you do it (for instance with the script below), and you try to
manually reduce/increase the size of the window, you should see
scrollbars at the edge of the window ; these scrollbars come from the
grid, because they take in account the labels row and col (wxGrid comes
from wxScrolledWindow). You can see that no more space is available
beyond the scrollbar, so the grid takes the whole space.
And why the white space on the right ? This space is not on the right of
the grid, but on the right of the last col. We could think it's like
that because it's not possible to compute an appropriate col size for
the grid cols, but it's not displayed exactly the same in linux and in
windows. In one case it's over the last col (not enough space) and in
the other case it's beyond (too much space). I think that as the program
must work on all the environments, the interface must stay enough
global, and sometimes the display is not perfectly done.
The advantage is that if we let wx decide, we won't have to think how to
set the widgets.
Regards,
jm

#----------------------------------------------------------------------
import wx,wx.grid
#----------------------------------------------------------------------
class MainWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,parent,wx.ID_ANY,title)
#--
self.grid= wx.grid.Grid(id=wx.ID_ANY,parent=self)
self.grid.CreateGrid(numRows=10,numCols=2)
self.grid.Fit()
self.Fit()
#--
self.Show(1)
#----------------------------------------------------------------------
app = wx.PySimpleApp()
frame=MainWindow(None,-1,'Grid sizer')
app.MainLoop()
del app
#----------------------------------------------------------------------

Thanks Tim,

I knew it was something like that, but I had to ask the question. I
was hoping that there was a way around the problem.

Roger
 
R

rbann11

jean-michel bain-cornu said:
(e-mail address removed) a écrit :
Hi Roger,
A key point is that the Grid manages itself its available space
according to the size it can have.
If you just tried to create a simple grid within a simple frame, you
probably got a grid filling all the frame space, and it's what you want.
Why that ?
If you do it (for instance with the script below), and you try to
manually reduce/increase the size of the window, you should see
scrollbars at the edge of the window ; these scrollbars come from the
grid, because they take in account the labels row and col (wxGrid comes
from wxScrolledWindow). You can see that no more space is available
beyond the scrollbar, so the grid takes the whole space.
And why the white space on the right ? This space is not on the right of
the grid, but on the right of the last col. We could think it's like
that because it's not possible to compute an appropriate col size for
the grid cols, but it's not displayed exactly the same in linux and in
windows. In one case it's over the last col (not enough space) and in
the other case it's beyond (too much space). I think that as the program
must work on all the environments, the interface must stay enough
global, and sometimes the display is not perfectly done.
The advantage is that if we let wx decide, we won't have to think how to
set the widgets.
Regards,
jm

#----------------------------------------------------------------------
import wx,wx.grid
#----------------------------------------------------------------------
class MainWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,parent,wx.ID_ANY,title)
#--
self.grid= wx.grid.Grid(id=wx.ID_ANY,parent=self)
self.grid.CreateGrid(numRows=10,numCols=2)
self.grid.Fit()
self.Fit()
#--
self.Show(1)
#----------------------------------------------------------------------
app = wx.PySimpleApp()
frame=MainWindow(None,-1,'Grid sizer')
app.MainLoop()
del app
#----------------------------------------------------------------------

Thanks Tim,

I knew it was something like that, but I had to ask the question. I
was hoping that there was a way around the problem.

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,299
Messages
2,571,546
Members
48,303
Latest member
BethRettig

Latest Threads

Top