New to Tkinter GUI building

A

Adam

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
 
A

Adonis Vargas

Adam wrote:
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)
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
 
A

Adam

Adam wrote:






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?
 
A

Adonis Vargas

Adam said:
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
 
A

Adam

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
 
E

Eric Brunel

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
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top