TKinter Python GUI alignment - python

I have the following code for my GUI:
root = Tk()
draft = Frame(root)
draft.grid()
root.title("test")
var = StringVar()
emailLabel = Label(draft, text="E-Mail:")
emailLabel.grid(row = 0, column = 0)
email = Entry(draft, justify=LEFT)
email.grid(row = 0, column = 1)
email.insert(0, "E-Mail")
passLabel = Label(draft, text="Pass:")
passLabel.grid(row = 1, column = 0)
password = Entry(draft, justify=LEFT, show="*")
password.grid(row = 1, column = 1)
password.insert(0, "Password")
start = Button(draft, text = "Start")
start.grid(row = 2, column = 0)
stop = Button(draft, text = "Stop")
stop.grid(row = 2, column = 1)
status = Text(draft)
status.grid(row = 3)
status.insert(INSERT, "TESTING")
However, it's not lining up the way I want it. I want the label and textboxes aligned to the left not the right, and the status text box to take up the entire size of the bottom (It's more or less a log).
Here's a screenshot:

To make the Text widget span over multiple columns, use the columnspan argument:
status.grid(row=3, columnspan=2)
Additionally, you can use the sticky parameter in the grid method to make the widgets occupy as many space as possible. Use this in every grid call:
widget.grid(..., sticky=N+W+E+S)
If you want the labels to be smaller, you have to rethink your layout a bit.
(Little note: it's not suggested to import * from tkinter. import tkinter as tk is preferred.)

Related

Why can't I specific the default selected radiobutton?

I am making a simple GUI using Python's tkinter module, and I'm having a lot of trouble with radiobuttons. I wish to have the first selected by default, but the other two are selected at the start. Additionally, when I just pass the cursor over the window, the first one becomes checked (I do not click) so all 3 show as selected. My code:
import tkinter as tk
class openGUI(object):
def __init__(self):
# title of window
window.title("DUNE LArTPC Simulator")
# label for choices
self.question = tk.Label(window, text = "Do you want to create new or analyse existing data?")
self.question.grid(row = 0, column = 0, columnspan = 3)
# buttons corresponding to choices
self.createBtn = tk.Button(window, text = "Create", command = self.createData)
self.analyseBtn = tk.Button(window, text = "Analyse", command = self.analyseData)
self.createBtn.grid(row = 1, column = 0)
self.analyseBtn.grid(row = 1, column = 2)
def analyseData(self):
"""
Not implemented yet.
"""
pass
def createData(self):
# edit window to display new widgets (irritating to have lots of windows open!)
window.title("Select variable")
self.question.destroy()
self.createBtn.destroy()
self.analyseBtn.destroy()
# text in window
variableQ = tk.Label(window, text = "Please select Independent variable for dataset:")
variableQ.grid(row = 0, column = 0, columnspan = 3)
# radioselect variable
selection = tk.StringVar()
selection.set("lifetime")
# radioselect buttons
lifetimeBtn = tk.Radiobutton(window, variable = selection, value = "lifetime", text = "Lifetime")
elecNoiseBtn = tk.Radiobutton(window, variable = selection, value = "electronic", text = "Electronic Noise")
radioactivityBtn = tk.Radiobutton(window, variable = selection, value = "radioactive", text = "Radioactivity")
lifetimeBtn.grid(row = 1, column = 0)
elecNoiseBtn.grid(row = 1, column = 1)
radioactivityBtn.grid(row = 1, column = 2)
# create window
window = tk.Tk()
# create class object with methods to populate
# window with widgets
initWin = openGUI()
# enter mainloop
window.mainloop()
Running the above gives me:
I have tried using lifetimeBtn.select() method instead of setting the StringVar(), but this does not seem to work either. What have I missed?
EDIT: added rest of code to show how I am using class and functions to manipulate the window.
It is because selection is a local variable inside createData() function and it will be garbage collected after function completes.
Change selection to instance variable self.selection.

Python Tkinter: Is there a way prevent 'Label' text from pushing columns to the side?

I'm pretty new to Python's Tkinter library tool. Currently I'm trying to output text on a screen when a user enters proper login credentials to a server node. I'm using grid instead of pack when I organize things around this GUI.
Is there a way to prevent Label text from pushing columns to the side?
This is what the current GUI looks like:
... and here's what happens when I "login" to the server node with some confirmation text on the screen:
... and this is what I'm wishing would happen instead (photoshopped):
... and using #Bryan Oakley's feedback (small difference):
I could move the text on the second column, column=1, but there are things to the right of textbox entry modules (that I won't show in this question). My initial thought that perhaps there is some layer priority with GUIs so maybe I could set the text at the bottom layers so that it wouldn't affect the textboxes but I haven't found anything with Tkinter's official documentation on how to do this.
Here's some of my code:
import json
import requests
import sys
import os
import time
import threading
import getpass
import warnings
from tkinter import *
from tkinter import ttk
nodeIPLabel = Label(text = 'Node IP/Hostname : ')
nodeIPLabel.grid(column = 0, row = 0, sticky='W')
nodeUsernameLabel = Label(text = 'Node username : ')
nodeUsernameLabel.grid(column = 0, row = 1, sticky='W')
nodePasswordLabel = Label(text = 'Node passwword : ')
nodePasswordLabel.grid(column = 0, row = 2, sticky='W')
# Enter Login Credentials
nodeIP = StringVar()
nodeUsername = StringVar()
nodePassword = StringVar()
nodeIPEntry = Entry(textvariable = nodeIP, width = 30)
nodeIPEntry.grid(column = 1, row = 0, sticky='W')
nodeUsernameEntry = Entry(textvariable = nodeUsername, width = 30)
nodeUsernameEntry.grid(column = 1, row = 1, sticky='W')
nodePasswordEntry = Entry(textvariable = nodePassword, width = 30, show = '*')
nodePasswordEntry.grid(column = 1, row = 2, sticky='W')
def nodeLogin():
global nodeIP
nodeIP = nodeIP.get()
global nodeUsername
nodeUsername = nodeUsername.get()
global nodePassword
nodePassword = nodePassword.get()
global nodeType
nodeType = nodeType.get()
nodeURL = 'https://' + nodeIP
try:
listOfStuff = '%s/redfish/v1/Chassis/1' % nodeURL
response = requests.get(listOfStuff, auth=(nodeUsername, nodePassword), verify=False)
if response.status_code == 200:
print('Connection successful!')
except requests.exceptions.ConnectionError:
connectionStatus = Label(text='Connection failure: Incorrect or nonexisting IP/Hostname. Hit "RESET GUI" to restart.', fg = 'red')
connectionStatus.grid(column = 0, row = 7, sticky='N')
except requests.exceptions.InvalidURL:
connectionStatus = Label(text='Connection failure: Nothing was typed in. Hit "RESET GUI" to restart.', fg = 'red')
connectionStatus.grid(column = 0, row = 7, sticky='N')
except KeyError:
connectionStatus = Label(text='Connection failure: Wrong login credentials. Hit "RESET GUI" to restart.', fg = 'red')
connectionStatus.grid(column = 0, row = 7, sticky='N')
except json.decoder.JSONDecodeError:
connectionStatus = Label(text='Connection failure: Unknown error... Hit "RESET GUI" to restart.', fg = 'red')
connectionStatus.grid(column = 0, row = 7, sticky='N')
def resetGUI():
os.startfile(__file__)
sys.exit()
# Login button
nodeLoginButton = Button(text = 'LOGIN', command = nodeLogin, bg = 'green', width = 20)
nodeLoginButton.grid(column = 0, row = 4, sticky='W')
# Reset script button
resetButton = Button(text = 'RESET GUI', command = resetGUI, bg = 'yellow', width = 20)
resetButton.grid(column = 0, row = 5, sticky = 'W')
# Stop script button
stopButton = Button(text = 'EMERGENCY STOP', command = window.destroy, bg = 'red', width = 20)
stopButton.grid(column = 0, row = 6, sticky = 'W')
window.mainloop()
The simplest solution in this specific case is to have that bottom label span more than one column.
connectionStatus.grid(column = 0, row = 7, sticky='N', columnspan=2)
Of course, that assumes you're only using 2 columns. If you're using more, adjust columnspan to fit your overall design.
However, even that's not enough if the text is literally too long to fit in the window. In that case, you need to give the widget a fixed size. When you do that, the contents of the label won't cause the label to change size.
Typically what I do for a widget like this is give it a size of one, have the label span all of the columns you are using, and then use the sticky attribute to have the widget "stick" to the sides of the space allocated to it. That will cause the label to grow to fit the space, but because you've given an explicit requested size, changing the text won't change the size of the label.
That being said, an even simpler approach is to divide your window into two parts: the top part would be a frame with all of the widgets, and the bottom part with just the label. You can then use pack to arrange the top and bottom parts easily, and then you can use grid to lay out the widgets in the frame, but because the label is outside the frame it won't affect anything.

Multi-page window cannot highlight Entry text with callback

Recently I've changed the layout of my program to include a multi-page window similar to what is in the provided example.
In the original, two-window configuration I had a binding set on each window to highlight all of the text in the Entry widget, based on a condition (no condition present in the example). This was fine.
Upon upgrading to a multi-page window, I tried to combine the callback to highlight text by passing the relevant widget and calling widget.select_range(0, END) as it is done in the example. Now I can't seem to highlight any text on mouse-click.
In addition to this, I've also tested my example code with having a separate callback for each Entry; even this would not highlight the text in the Entry upon clicking on it.
Could this have something to do with lifting frames & where the focus lies? As a test I've added a similar callback for "submitting" the Entry value, and this is working fine. At this point I'm confused as to why this wouldn't work. Any help is greatly appreciated.
UPDATE:
I forgot that to solve the highlighting problem, I've needed to include a return "break" line in the callback that is used to highlight the text.
Now, with this included, I have some very strange behavior with the Entry widgets. I can't click on them unless they have been focused using the tab key.
Is there any way to work around this problem?
Here is the example code I have been playing with (with the updated return statement):
from Tkinter import *
class Window():
def __init__(self, root):
self.root = root
self.s1 = StringVar()
self.s1.set("")
self.s2 = StringVar()
self.s2.set("")
# Frame 1
self.f1 = Frame(root, width = 50, height = 25)
self.f1.grid(column = 0, row = 1, columnspan = 2)
self.page1 = Label(self.f1, text = "This is the first page's entry: ")
self.page1.grid(column = 0, row = 0, sticky = W)
self.page1.grid_columnconfigure(index = 0, minsize = 90)
self.val1 = Label(self.f1, text = self.s1.get(), textvariable = self.s1)
self.val1.grid(column = 1, row = 0, sticky = E)
self.l1 = Label(self.f1, text = "Frame 1 Label")
self.l1.grid(column = 0, row = 1, sticky = W)
self.e1 = Entry(self.f1, width = 25)
self.e1.grid(column = 1, row = 1, sticky = E)
self.e1.bind("<Button-1>", lambda event: self.event(self.e1))
self.e1.bind("<Return>", lambda event: self.submit(self.e1, self.s1))
# Frame 2
self.f2 = Frame(root, width = 50, height = 25)
self.f2.grid(column = 0, row = 1, columnspan = 2)
self.page2 = Label(self.f2, text = "This is the 2nd page's entry: ")
self.page2.grid(column = 0, row = 0, sticky = W)
self.page2.grid_columnconfigure(index = 0, minsize = 90)
self.val2 = Label(self.f2, text = self.s2.get(), textvariable = self.s2)
self.val2.grid(column = 1, row = 0, sticky = E)
self.l2 = Label(self.f2, text = "Frame 2 Label")
self.l2.grid(column = 0, row = 1, sticky = W)
self.e2 = Entry(self.f2, width = 25)
self.e2.grid(column = 1, row = 1, sticky = E)
self.e2.bind("<Button-1>", lambda event: self.event(self.e2))
self.e2.bind("<Return>", lambda event: self.submit(self.e2, self.s2))
self.b1 = Button(root, width = 15, text = "Page 1", command = lambda: self.page(1), relief = SUNKEN)
self.b1.grid(column = 0, row = 0, sticky = E)
# Buttons
self.b2 = Button(root, width = 15, text = "Page 2", command = lambda: self.page(2))
self.b2.grid(column = 1, row = 0, sticky = W)
# Start with Frame 1 lifted
self.f1.lift()
def page(self, val):
self.b1.config(relief = RAISED)
self.b2.config(relief = RAISED)
if val == 1:
self.f1.lift()
self.b1.config(relief = SUNKEN)
else:
self.f2.lift()
self.b2.config(relief = SUNKEN)
def event(self, widget):
widget.select_range(0, END)
return "break"
def submit(self, widget, target):
target.set(widget.get())
root = Tk()
w = Window(root)
root.mainloop()
Well, this has been a productive question. If anyone in the future is doing something similar to this and needs a reference for how to solve the problem:
I was able to work around the problem by forcing the Entry widgets into focus every time I switch frames, and using the return "break" statement that I mention in the question's update.
This isn't ideal, as every time a page is changed you automatically focus on the Entry widget, but once the widget is in focus it's behavior is exactly what I would expect so this isn't of great concern. In my program, if you are changing pages it is quite likely you will use the Entry widget anyway (it is a search entry).
Here's the final changes required to make the code work correctly:
# .... some code ....
self.f1.lift()
self.e1.focus_force()
def page(self, val):
self.b1.config(relief = RAISED)
self.b2.config(relief = RAISED)
if val == 1:
self.f1.lift()
self.b1.config(relief = SUNKEN)
self.e1.focus_force()
else:
self.f2.lift()
self.b2.config(relief = SUNKEN)
self.e2.focus_force()
def event(self, widget):
widget.select_range(0, END)
return "break"
# .... more code ....

Tkinter / Pmw -- Centering Frame Inside Notebook Frame

What I'm trying to do is get the frame with the two buttons (sframe) centered inside of the notebook (master) frame. This works without issue on Python 2.4 but on Python 2.7 the frame is anchored to NW by default. I know if I rowconfigure() / columnconfigure() the master page frame the inner frame will center itself but this solution doesn't seem correct. Disabling propagation and changing row/column weights don't seem to help either. Is there anyway to just get that inner frame centered properly? Here is the test code I'm working with:
import Tkinter as tk, Tkinter
import Pmw
class SimpleApp(object):
def __init__(self, master, **kwargs):
title = kwargs.pop('title')
master.configure(bg='blue')
sframe = tk.Frame(master, relief=tk.RIDGE, bd=5, width=100,bg='green')
sframe.grid()
button = tk.Button(sframe, text = title)
button.grid(sticky = tk.W)
button = tk.Button(sframe, text = 'next')
button.grid(sticky = tk.E)
#sframe.propagate(0)
#master.rowconfigure(0, minsize = 300)
#master.columnconfigure(0, minsize = 300)
class Demo:
def __init__(self, parent):
# Create and pack the NoteBook.
notebook = Pmw.NoteBook(parent)
notebook.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
# Add the "Appearance" page to the notebook.
page = notebook.add('Helpers')
app = SimpleApp(page, title= 'hello, world')
notebook.tab('Helpers').focus_set()
page = notebook.add('Appearance')
# Create the "Toolbar" contents of the page.
group = Pmw.Group(page, tag_text = 'Toolbar')
group.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
b1 = Tkinter.Checkbutton(group.interior(), text = 'Show toolbar')
b1.grid(row = 0, column = 0)
b2 = Tkinter.Checkbutton(group.interior(), text = 'Toolbar tips')
b2.grid(row = 0, column = 1)
# Create the "Startup" contents of the page.
group = Pmw.Group(page, tag_text = 'Startup')
group.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
home = Pmw.EntryField(group.interior(), labelpos = 'w',
label_text = 'Home page location:')
home.pack(fill = 'x', padx = 20, pady = 10)
page = notebook.add('Images')
notebook.setnaturalsize()
def basic():
root = tk.Tk()
#app = SimpleApp(root, title = 'Hello, world')
app = Demo(root)
root.mainloop()
basic()
Let me know if I can provide any additional information.
You need to configure the weight of row 0 and column 0 in the master:
master.grid_columnconfigure(0, weight=1)
master.grid_rowconfigure(0, weight=1)
You are placing that inner sframe in row 0 column 0 of master, and since that cell has no weight it shrinks up to the upper-left corner. Giving the row and column a weight of 1 makes the column and row fill the available space. Since you aren't using any sticky options for the sframe, it will stay centered in its cell rather than filling its cell.

Why is this code working I took from a textbook for GUI Python?

Why isn't this working. This is straight from the text book. I'm getting an Attribute error saying self._area does not exist.
from Tkinter import *
import math
class CircleArea(Frame):
def __init__(self):
"""Sets up a window and widgets."""
Frame.__init__(self)
self.master.title("Circle Area")
self.grid()
#Label and field for radius
self._radiusLabel = Label(self, text = "Radius")
self._radiusLabel.grid(row = 0, column = 0)
self._radiusVar = DoubleVar()
self._radiusEntry = Entry(self, textvariable = self._radiusVar)
self._radiusEntry.grid(row = 0, column = 1)
#Label and field for the area
self._areaLabel = Label(self, text = "Area")
self._areaLabel.grid(row = 1, column = 0)
self._areaVar = DoubleVar()
self._areaEntry = Entry(self, textvariable = self._areaVar)
self._areaEntry.grid(row = 1, column = 1)
# The command button
self._button = Button(self, text = "Compute", command = self._area)
self._button.grid(row = 2, column = 0, columnspan = 2)
def _area(self):
"""Event handler for button."""
radius = self._radiusVar.get()
area = radius ** 2 * math.pi
self._areaVar.set(area)
def main():
CircleArea(). mainloop()
run = CircleArea()
run.main()
Is it because the _area method is declared after it is called? That doesn't make sense why it wouldn't work using a down up programming technique. I'm really new to GUI just started learning. First chapter on GUI for class.
edit*: I'm expecting a window to pop up and have one Entry field for input for the radius of the circle. With a label Radius. And an output entry field for the results of the area of the circle based on the radius. and a compute button at the bottom which computes it.
And I just wanted to get used to typing the different commands and such. I haven't even been in the lecture for this yet. I was just seeing what this code would do and what it would look like. I typed it all out by hand if that makes you feel better.:P Instead of copy and pasting.
The problem is that your indenting is wrong. _area and main are defined within __init__, which you don't want. Correct indenting is below (you don't need a main function).
from Tkinter import *
import math
class CircleArea(Frame):
def __init__(self):
"""Sets up a window and widgets."""
Frame.__init__(self)
self.master.title("Circle Area")
self.grid()
#Label and field for radius
self._radiusLabel = Label(self, text = "Radius")
self._radiusLabel.grid(row = 0, column = 0)
self._radiusVar = DoubleVar()
self._radiusEntry = Entry(self, textvariable = self._radiusVar)
self._radiusEntry.grid(row = 0, column = 1)
#Label and field for the area
self._areaLabel = Label(self, text = "Area")
self._areaLabel.grid(row = 1, column = 0)
self._areaVar = DoubleVar()
self._areaEntry = Entry(self, textvariable = self._areaVar)
self._areaEntry.grid(row = 1, column = 1)
# The command button
self._button = Button(self, text = "Compute", command = self._area)
self._button.grid(row = 2, column = 0, columnspan = 2)
def _area(self):
"""Event handler for button."""
radius = self._radiusVar.get()
area = radius ** 2 * math.pi
self._areaVar.set(area)
run = CircleArea()
run.mainloop()
Actually I think you miss an argument in your main method,you define a class CircleArea , but in python you know that, each method defined in class must have an default argument named 'self',so just try this
def main(self):
CircleArea(). mainloop()
I think it will work as you wish :)

Categories

Resources