473,842 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to Tkinter GUI building

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.t op, width=700, height=400)
self.labAll = tk.Label(self.m ainWindow, text="All Files/
Folders:")
self.labVi = tk.Label(self.m ainWindow, text="Violating Files/
Folders:")

#Create a sub-frame containing textbox and scrollbar for All
files scanned display
self.allTxtf = tk.Frame(self.m ainWindow, width=699,
height=164)
self.scrlr1 = tk.Scrollbar(se lf.allTxtf)
self.txtBoxall = tk.Text(self.al lTxtf, wrap=tk.CHAR,
yscrollcommand= self.scrlr1.set )
self.scrlr1.con fig(command = self.txtBoxall. yview)

#Create another sub-frame containing textbox and scrollbar for
the Violating files display
self.viTxtf = tk.Frame(self.m ainWindow, width=699, height=164)
self.scrlr2 = tk.Scrollbar(se lf.viTxtf)
self. txtBoxvi = tk.Text(self.vi Txtf, wrap=tk.CHAR,
yscrollcommand= self.scrlr2.set )
self.scrlr2.con fig(command = self.txtBoxvi.y view)

#Create another sub-frame to contain the controls
self.ctrlFrame = tk.Frame(self.m ainWindow, width=699,
height=72)
self.labDir = tk.Label(self.c trlFrame, text="Dir:")
self.entDir = tk.Entry(self.c trlFrame)
self.labChar = tk.Label(self.c trlFrame, text="Char. Limit:")
self.entChar = tk.Entry(self.c trlFrame)
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.gri d(row=0)
self.allTxtf.gr id(row=1)
self.labVi.grid (row=2)
self.viTxtf.gri d(row=3)
self.ctrlFrame. grid(row=4)
#Now allign the content of allTxtf
self.txtBoxall. grid(row=0, column=0)
self.scrlr1.gri d(row=0, column=1)
#Now allign the content for viTxtf
self.txtBoxvi.g rid(row=0, column=0)
self.scrlr2.gri d(row=0, column=1)
#Now allign the content for ctrlFrame
self.labDir.gri d(row=0, column=0, sticky=tk.E)
self.entDir.gri d(row=0, column=1)
self.labChar.gr id(row=0, column=2, sticky=tk.E)
self.entChar.gr id(row=0, column=3)
self.btFind.gri d(row=0, column=4)
self.btExit.gri d(row=0,column= 5)

def findallfiles(se lf, 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
6 2517
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.geomet ry("%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.gri d(row=0, column=1, sticky=tk.N + tk.S)

Hope this helps.

Adonis
Feb 28 '07 #2
On Feb 28, 9:13 pm, Adonis Vargas <ado...@REMOVET HISearthlink.ne t>
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.geomet ry("%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.gri d(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
Adam wrote:
On Feb 28, 9:13 pm, Adonis Vargas <ado...@REMOVET HISearthlink.ne t>
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.geomet ry("%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.gri d(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.geomet ry("%dx%d%+d%+d " % (800, 600, 0, 0))

Hope this helps.

Adonis
Mar 1 '07 #4
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.geomet ry("%dx%d%+d%+d " % (700, 400, 0, 0)) #
(width,height, x, y)
self.mainWindow = tk.Frame(self.t op)#width=700, height=400)
self.labAll = tk.Label(self.m ainWindow, text="All Files/
Folders:")
self.labVi = tk.Label(self.m ainWindow, text="Violating Files/
Folders:")

#Create a sub-frame containing textbox and scrollbar for All
files scanned display
self.allTxtf = tk.Frame(self.m ainWindow, width=690,
height=164)
self.scrlr1 = tk.Scrollbar(se lf.allTxtf)
self.txtBoxall = tk.Text(self.al lTxtf, wrap=tk.CHAR,
yscrollcommand= self.scrlr1.set )
self.scrlr1.con fig(command = self.txtBoxall. yview)

#Create another sub-frame containing textbox and scrollbar for
the Violating files display
self.viTxtf = tk.Frame(self.m ainWindow, width=690, height=164)
self.scrlr2 = tk.Scrollbar(se lf.viTxtf)
self. txtBoxvi = tk.Text(self.vi Txtf, wrap=tk.CHAR,
yscrollcommand= self.scrlr2.set )
self.scrlr2.con fig(command = self.txtBoxvi.y view)

#Create another sub-frame to contain the controls
self.ctrlFrame = tk.Frame(self.m ainWindow, width=690,
height=72)
self.labDir = tk.Label(self.c trlFrame, text="Dir:")
self.entDir = tk.Entry(self.c trlFrame)
self.labChar = tk.Label(self.c trlFrame, text="Char. Limit:")
self.entChar = tk.Entry(self.c trlFrame, textvariable="2 54")
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.gri d(row=0)
self.allTxtf.gr id(row=1)
self.labVi.grid (row=2)
self.viTxtf.gri d(row=3)
self.ctrlFrame. grid(row=4)
#Now allign the content of allTxtf
self.txtBoxall. grid(row=0, column=0)
self.scrlr1.gri d(row=0, column=1, sticky=tk.N + tk.S)
#Now allign the content for viTxtf
self.txtBoxvi.g rid(row=0, column=0)
self.scrlr2.gri d(row=0, column=1, sticky=tk.N + tk.S)
#Now allign the content for ctrlFrame
self.labDir.gri d(row=0, column=0, sticky=tk.E)
self.entDir.gri d(row=0, column=1)
self.labChar.gr id(row=0, column=2, sticky=tk.E)
self.entChar.gr id(row=0, column=3)
self.btFind.gri d(row=0, column=4)
self.btExit.gri d(row=0,column= 5)

def findallfiles(se lf, base):
Pass

def fillboxes(self) :
Pass

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

#####END#####

Regards,
Adam

Mar 1 '07 #5
On Thu, 01 Mar 2007 21:01:40 +0100, Adam <ad**********@g ooglemail.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_rowconfigu re and grid_columnconf igure
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('700x 400+0+0')
self.labAll = tk.Label(self.m ainWindow, text="All Files/Folders:")
...

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"
Mar 2 '07 #6
Thanks for the reply, will work with this tomorrow.

Adam

Mar 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3374
by: Dennis Sylvester | last post by:
Interested in any opinions, studies, etc., concerning the GUI tool(s) to use for Python. Specifically, which would you recommend (and why) for building GUI apps in Python: TKinter BOA PyQT Thanks, Dennis
3
4070
by: Rob Andrews | last post by:
I'm on a Red Hat 9 system, which has Python 2.2.2 installed, and I installed 2.3 separately into /home/rob/Python-2.3/ (creating the symbolic link "py23" to point to my 2.3 installation). Now I'm trying to work out the kinks in the process. Unable to run Idle using 2.3 the way I've got things set up, I created a super simple Tkinter test program that just pops up a Label widget. I pasted below an example of how the RH-provided 2.2 runs...
0
1318
by: Abstractius | last post by:
Hello, I was able to unearth some ancient documentation suggesting Modules/Setup has to be edited by hand (after ./configure) to compile tkinter. The short info at http://www.python.org/topics/tkinter/download.html doesn't mention this, instead it is a bit contradictory first stating that tk is part of python and then that Unix users must download and build Tcl and Tk (which I did for the latest versions 8.4.4). In all cases, I wasn't...
0
1676
by: Rob Dijkshoorn | last post by:
Hi All, I have some problems building the python modules, most notably Tkinter, on AIX 5.1. Can anyone be of any assistance? Thanks, Rob This is what the compiler spits out:
5
3698
by: Andrew | last post by:
Hi I was wondering if there is anyway in Tkinter to create GUIs using Graphics, like windows media player or other tools like that basically so the interface wouldn't be your standard interface any ideas, links to tutorials, or examples is appreciated Cheers
11
2096
by: Harlin Seritt | last post by:
There are certain options for Tkinter widgets that have default values that I don't much care for (borderwidth, font come to mind) and continuously change when I'm building interfaces. With a bit of tweaking I have been able to give the widgets a look that rivals the best of them. However, I get tired of doing this when I'm writing code and would like a way that I could universally change them on my system. I have tried to find where in...
2
5218
by: Andrew Trevorrow | last post by:
Our app uses embedded Python to allow users to run arbitrary scripts. Scripts that import Tkinter run fine on Windows, but on Mac OS X there is a serious problem. After a script does "root = Tk()" our app's menus are permanently changed in the following way: - The top item in the application menu changes to "About Tcl & Tk...". - The Quit item is disabled. - The File and Edit menus are completely replaced. - All further menus (except...
3
2654
by: frikk | last post by:
Hey everyone. I have been working with python for a couple years now, but just recently built my first program with a GUI. I decided to start with Tkinter since it is included with the base package, although wxWindows will likely be my next choice. Tkinter seems to be pretty slow for my needs. Anyway - I am building a genetic algorithm simulator. I have a grid where an Ant moves around. It is infeasible for me to update the grid...
3
2400
by: Miki | last post by:
Hello, Tk.lift doesn't seem to work on OSX (Python 2.5.1). The below starts OK, but the window is the behind all other windows. from Tkinter import * root = Tk() Button(root, text="OK", command=root.quit).pack() root.lift()
4
5000
by: njwilson23 | last post by:
I'm having trouble with tkinter on a new installation of Python (2.6), built with the framework option from source that was downloaded from python.org. I'm running OS 10.4 on a PowerPC G4. The problem first arose when I tried to run matplotlib - it couldn't find tcl/tk because it was searching for 8.5, and I had 8.4. I found and built tcl/tk 8.5, which led to a new error, reproduced below: Traceback (most recent call last): File...
0
9872
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10945
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10612
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10314
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7038
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.