By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,900 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,900 IT Pros & Developers. It's quick & easy.

New to Tkinter GUI building

P: n/a
Hey,

I'm pretty new to programming. Been trying to learn using Python.
The code I'm struggling with is for my GUI.

I'm am having trouble getting this to display the way I want with the
grid manager. Can anybody tell me what I am doing wrong? I hope you
can tell what look I'm trying to achieve from the code as its hard to
explain.

I think my main questions are:
1. How can I get the Window to be sized the way I want it?
2. How can I get the Scrollbars to fill the side of the text box
instead of being small? (like .pack(fill= tk.Y)

######Code#######

#file/path finder
#indentation value 4
import os,glob
import Tkinter as tk

# Class to create the Application and UI
class Theapp:
def __init__(self):
# Create GUI parts. Will allign later with grid
self.top = tk.Tk()
self.mainWindow = tk.Frame(self.top, width=700, height=400)
self.labAll = tk.Label(self.mainWindow, text="All Files/
Folders:")
self.labVi = tk.Label(self.mainWindow, text="Violating Files/
Folders:")

#Create a sub-frame containing textbox and scrollbar for All
files scanned display
self.allTxtf = tk.Frame(self.mainWindow, width=699,
height=164)
self.scrlr1 = tk.Scrollbar(self.allTxtf)
self.txtBoxall = tk.Text(self.allTxtf, wrap=tk.CHAR,
yscrollcommand=self.scrlr1.set)
self.scrlr1.config(command = self.txtBoxall.yview)

#Create another sub-frame containing textbox and scrollbar for
the Violating files display
self.viTxtf = tk.Frame(self.mainWindow, width=699, height=164)
self.scrlr2 = tk.Scrollbar(self.viTxtf)
self. txtBoxvi = tk.Text(self.viTxtf, wrap=tk.CHAR,
yscrollcommand=self.scrlr2.set)
self.scrlr2.config(command = self.txtBoxvi.yview)

#Create another sub-frame to contain the controls
self.ctrlFrame = tk.Frame(self.mainWindow, width=699,
height=72)
self.labDir = tk.Label(self.ctrlFrame, text="Dir:")
self.entDir = tk.Entry(self.ctrlFrame)
self.labChar = tk.Label(self.ctrlFrame, text="Char. Limit:")
self.entChar = tk.Entry(self.ctrlFrame)
self.btFind = tk.Button(self.ctrlFrame, text="Scan", command =
self.fillboxes)
self.btExit = tk.Button(self.ctrlFrame, text="Exit", command =
self.quitEvent)
#Use tkinter's grid geometry manager to allign and display the
GUI
self.mainWindow.grid()
#Frist allign the 3 main frames
self.labAll.grid(row=0)
self.allTxtf.grid(row=1)
self.labVi.grid(row=2)
self.viTxtf.grid(row=3)
self.ctrlFrame.grid(row=4)
#Now allign the content of allTxtf
self.txtBoxall.grid(row=0, column=0)
self.scrlr1.grid(row=0, column=1)
#Now allign the content for viTxtf
self.txtBoxvi.grid(row=0, column=0)
self.scrlr2.grid(row=0, column=1)
#Now allign the content for ctrlFrame
self.labDir.grid(row=0, column=0, sticky=tk.E)
self.entDir.grid(row=0, column=1)
self.labChar.grid(row=0, column=2, sticky=tk.E)
self.entChar.grid(row=0, column=3)
self.btFind.grid(row=0, column=4)
self.btExit.grid(row=0,column=5)

def findallfiles(self, base):
pass

def fillboxes(self):
pass

def quitEvent(self):
raise SystemExit

app = Theapp()
app.mainWindow.mainloop()

######End Code#######

I have only posted the code relevant to the GUI.

TIA
Adam

Feb 28 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Adam wrote:
<snip>
I think my main questions are:
1. How can I get the Window to be sized the way I want it?
2. How can I get the Scrollbars to fill the side of the text box
instead of being small? (like .pack(fill= tk.Y)
<snip>
>
I have only posted the code relevant to the GUI.

TIA
Adam
To size the window use Tk's geometry method

self.top.geometry("%dx%d%+d%+d" % (800, 600, 0, 0)) # (width,
height, x, y)

For the scrollbar to fill vertically, use the sticky grid option.

self.scrlr1.grid(row=0, column=1, sticky=tk.N + tk.S)

Hope this helps.

Adonis
Feb 28 '07 #2

P: n/a
On Feb 28, 9:13 pm, Adonis Vargas <ado...@REMOVETHISearthlink.net>
wrote:
Adam wrote:

<snip>
I think my main questions are:
1. How can I get the Window to be sized the way I want it?
2. How can I get the Scrollbars to fill the side of the text box
instead of being small? (like .pack(fill= tk.Y)

<snip>
I have only posted the code relevant to the GUI.
TIA
Adam

To size the window use Tk's geometry method

self.top.geometry("%dx%d%+d%+d" % (800, 600, 0, 0)) # (width,
height, x, y)

For the scrollbar to fill vertically, use the sticky grid option.

self.scrlr1.grid(row=0, column=1, sticky=tk.N + tk.S)

Hope this helps.

Adonis
Can't test now as its late in th UK and I'm going to bed. Looks good
though.
So remove the size from the frames etc and use the geometry method
instead? Then use grid to "pack" them for want of a better word?

Feb 28 '07 #3

P: n/a
Adam wrote:
On Feb 28, 9:13 pm, Adonis Vargas <ado...@REMOVETHISearthlink.net>
wrote:
>Adam wrote:

<snip>
>>I think my main questions are:
1. How can I get the Window to be sized the way I want it?
2. How can I get the Scrollbars to fill the side of the text box
instead of being small? (like .pack(fill= tk.Y)
<snip>
>>I have only posted the code relevant to the GUI.
TIA
Adam
To size the window use Tk's geometry method

self.top.geometry("%dx%d%+d%+d" % (800, 600, 0, 0)) # (width,
height, x, y)

For the scrollbar to fill vertically, use the sticky grid option.

self.scrlr1.grid(row=0, column=1, sticky=tk.N + tk.S)

Hope this helps.

Adonis

Can't test now as its late in th UK and I'm going to bed. Looks good
though.
So remove the size from the frames etc and use the geometry method
instead? Then use grid to "pack" them for want of a better word?
No, the geometry method is used to set the size of your main application
window. This is what I understood from your first question, and please
correct me if I am wrong. The grid method (or the pack method) are used
to layout the widgets.

In other words, after line 8 of the code you provided you would add this
line:

self.top.geometry("%dx%d%+d%+d" % (800, 600, 0, 0))

Hope this helps.

Adonis
Mar 1 '07 #4

P: n/a
Ok the window has resized but the elements inside are still like they
were, so they are going off the edge on the window. How can I get
these to resize? I have put sizes on the frames they are in. Sorry to
keep asking but I'm flying blind here, I have checked the python site
and the intro to tkinter which are both usually good for this kinda
thing.

#####CODE#####

#file/path finder
#indentation value 4
import os,glob
import Tkinter as tk
# Class to create the Application and UI
class Theapp:
def __init__(self):
# Create GUI parts. Will allign later with grid
self.top = tk.Tk()
self.top.geometry("%dx%d%+d%+d" % (700, 400, 0, 0)) #
(width,height, x, y)
self.mainWindow = tk.Frame(self.top)#width=700, height=400)
self.labAll = tk.Label(self.mainWindow, text="All Files/
Folders:")
self.labVi = tk.Label(self.mainWindow, text="Violating Files/
Folders:")

#Create a sub-frame containing textbox and scrollbar for All
files scanned display
self.allTxtf = tk.Frame(self.mainWindow, width=690,
height=164)
self.scrlr1 = tk.Scrollbar(self.allTxtf)
self.txtBoxall = tk.Text(self.allTxtf, wrap=tk.CHAR,
yscrollcommand=self.scrlr1.set)
self.scrlr1.config(command = self.txtBoxall.yview)

#Create another sub-frame containing textbox and scrollbar for
the Violating files display
self.viTxtf = tk.Frame(self.mainWindow, width=690, height=164)
self.scrlr2 = tk.Scrollbar(self.viTxtf)
self. txtBoxvi = tk.Text(self.viTxtf, wrap=tk.CHAR,
yscrollcommand=self.scrlr2.set)
self.scrlr2.config(command = self.txtBoxvi.yview)

#Create another sub-frame to contain the controls
self.ctrlFrame = tk.Frame(self.mainWindow, width=690,
height=72)
self.labDir = tk.Label(self.ctrlFrame, text="Dir:")
self.entDir = tk.Entry(self.ctrlFrame)
self.labChar = tk.Label(self.ctrlFrame, text="Char. Limit:")
self.entChar = tk.Entry(self.ctrlFrame, textvariable="254")
self.btFind = tk.Button(self.ctrlFrame, text="Scan", command =
self.fillboxes)
self.btExit = tk.Button(self.ctrlFrame, text="Exit", command =
self.quitEvent)
#Use tkinter's grid geometry manager to allign and display the
GUI
self.mainWindow.grid()
#Frist allign the 3 main frames
self.labAll.grid(row=0)
self.allTxtf.grid(row=1)
self.labVi.grid(row=2)
self.viTxtf.grid(row=3)
self.ctrlFrame.grid(row=4)
#Now allign the content of allTxtf
self.txtBoxall.grid(row=0, column=0)
self.scrlr1.grid(row=0, column=1, sticky=tk.N + tk.S)
#Now allign the content for viTxtf
self.txtBoxvi.grid(row=0, column=0)
self.scrlr2.grid(row=0, column=1, sticky=tk.N + tk.S)
#Now allign the content for ctrlFrame
self.labDir.grid(row=0, column=0, sticky=tk.E)
self.entDir.grid(row=0, column=1)
self.labChar.grid(row=0, column=2, sticky=tk.E)
self.entChar.grid(row=0, column=3)
self.btFind.grid(row=0, column=4)
self.btExit.grid(row=0,column=5)

def findallfiles(self, base):
Pass

def fillboxes(self):
Pass

def quitEvent(self):
raise SystemExit
app = Theapp()
app.mainWindow.mainloop()

#####END#####

Regards,
Adam

Mar 1 '07 #5

P: n/a
On Thu, 01 Mar 2007 21:01:40 +0100, Adam <ad**********@googlemail.com
wrote:
Ok the window has resized but the elements inside are still like they
were, so they are going off the edge on the window. How can I get
these to resize? I have put sizes on the frames they are in. Sorry to
keep asking but I'm flying blind here, I have checked the python site
and the intro to tkinter which are both usually good for this kinda
thing.
[snip code]

I've not tried your code, but it seems to be missing some grid
configuration. Basically, you need to tell all containers what row(s) or
column(s) will grow or shrink when the container itself grows or shrink.
This is done by calling the grid_rowconfigure and grid_columnconfigure
methods on the container, passing the weight option:
container.grid_rowconfigure(0, weight=1)
means that when the container size changes, all rows will (try to) stay
the same, except row number 0 which will adapt its size to the new
container size. (This is actually a bit more complicated than that, but it
should suit your needs for the moment).

Since the default weight for all rows and columns is 0, meaning "don't
change your size", the behavior you see is quite normal.

A little tip: things are usually a bit more complicated when you have
several levels of frames within each other. Specifying a "flashy"
background color for frames can help you to figure out which one does
change its size as you want. Use bg='red' or bg='green' in your Frame
creations to make obvious where they are; you'll see far clearly what
happens when you resize your window.

A few short notes:
- You don't need at all to put all your widgets in attributes. Unless
you'll have to access the widget itself later (e.g to change it state or
color, or whatever), putting it in a local variable is fine.
- In your code, it appears to me that self.mainWindow is not needed. The
Tk instance (self.top) *is* the top-level window and is a valid container
for whatever widgets you need. Having a Frame inside it will only cause
layout problems. I'd do:
self.mainWindow = tk.Tk()
self.mainWindow.geometry('700x400+0+0')
self.labAll = tk.Label(self.mainWindow, text="All Files/Folders:")
...

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Mar 2 '07 #6

P: n/a
Thanks for the reply, will work with this tomorrow.

Adam

Mar 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.