Tkinter changing entry state based on radiobutton - python

I have four radio buttons. Underneath these four button is an Entry widget. I am trying to make this Entry widget only become available to type into when the last radio button is selected. The gui is in a class, as you can see in the code below:
class Gui:
def __init__(self):
pass
def draw(self):
global root
if not root:
root = tk.Tk()
root.geometry('280x350')
self.type = tk.StringVar()
self.type_label = tk.Label(text="Game Mode")
self.name_entry = tk.Entry()
self.name_entry.configure(state="disabled")
self.name_entry.update()
self.type_entry_one = tk.Radiobutton(text="Garage", value="garage", variable=self.type, command=self.disable_entry(self.name_entry))
self.type_entry_two = tk.Radiobutton(text="Festival", value="festival", variable=self.type, command=self.disable_entry(self.name_entry))
self.type_entry_three = tk.Radiobutton(text="Studio", value="studio", variable=self.type, command=self.disable_entry(self.name_entry))
self.type_entry_four = tk.Radiobutton(text="Rockslam", value="rockslam", variable=self.type, command=self.enable_entry(self.name_entry))
self.type_label.pack()
self.type_entry_one.pack()
self.type_entry_two.pack()
self.type_entry_three.pack()
self.type_entry_four.pack()
self.name_entry.pack()
root.mainloop()
def enable_entry(self, entry):
entry.configure(state="normal")
entry.update()
def disable_entry(self, entry):
entry.configure(state="disabled")
entry.update()
if __name__ == '__main__':
root = None
gui = Gui()
gui.draw()
However, the the self.name_entry is always available to type into. What am I doing wrong. If you still don't understand what is happening then please run this code yourself and you will see.
Thank you very much for your time and I look forward to responses.

You have the right idea about using the RadioButton to enable/disable the entry widget. Mostly it is your class design that is flawed - it is halfway between OO code, and procedural code...
I fixed the class structure and made it a subclass of tk.Tk so it completely encapsulate your GUI. The name_entry is now enabled only when type_entry_four radio button is selected, and disabled otherwise. I've set that last button to be selected at launch, but you can easily change that; it results in the entry being enabled at launch.
Superfluous variable passing through methods was removed, as was the draw method and the calls to it; all widget creation is now conveniently found in GUI.__init__
import tkinter as tk
class Gui(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('280x350')
self.select_type = tk.StringVar()
self.type_label = tk.Label(self, text='Game Mode')
self.name_entry = tk.Entry(self)
self.type_entry_one = tk.Radiobutton(self, text='Garage', value='garage', variable=self.select_type, command=self.disable_entry)
self.type_entry_two = tk.Radiobutton(self, text='Festival', value='festival', variable=self.select_type, command=self.disable_entry)
self.type_entry_three = tk.Radiobutton(self, text='Studio', value='studio', variable=self.select_type, command=self.disable_entry)
self.type_entry_four = tk.Radiobutton(self, text='Rockslam', value='rockslam', variable=self.select_type, command=self.enable_entry)
self.select_type.set('rockslam') # select the last radiobutton; also enables name_entry
self.type_label.pack()
self.type_entry_one.pack()
self.type_entry_two.pack()
self.type_entry_three.pack()
self.type_entry_four.pack()
self.name_entry.pack()
def enable_entry(self):
self.name_entry.configure(state='normal')
def disable_entry(self):
self.name_entry.configure(state='disabled')
if __name__ == '__main__':
Gui().mainloop()

The only problemS, I see, your facing here is because your not passing in the value "properly" into the function, when you use (..), your calling the function, so to get rid of that use lambda, like:
self.type_entry_one = tk.Radiobutton(text="Garage", value="garage", variable=self.type, command=lambda: self.disable_entry(self.name_entry))
self.type_entry_two = tk.Radiobutton(text="Festival", value="festival", variable=self.type, command=lambda:self.disable_entry(self.name_entry))
self.type_entry_three = tk.Radiobutton(text="Studio", value="studio", variable=self.type, command=lambda:self.disable_entry(self.name_entry))
self.type_entry_four = tk.Radiobutton(text="Rockslam", value="rockslam", variable=self.type, command=lambda:self.enable_entry(self.name_entry))
When using command=lambda:func(arg), this will get executed only when selecting a radiobutton. That is the point of using a radiobutton, right?
Also notice that when the initial code is run, the entire radiobuttons are selected, I think its probably because of tristate values, to get rid of that there are 2 ways I'm aware of:
Changing the declaration of self.type to:
self.type = tk.StringVar(value=' ')
Or, you could also go on adding an extra option to each radiobutton, tristatevalue=' ', like:
self.type_entry_one = tk.Radiobutton(text="Garage",..,tristatevalue=' ')
But make sure to do just one of the above solution. Take a read here about more on tristate values.
Also keep a note that your not passing in any master window to the widgets, its fine as long as your having just one window, when working with multiple windows, it may get confusing for where the widgets should appear.
Also side-note, if this is the complete code, then if nothing is being done on __init__(), its definition can be removed.

Related

tkinter button in class can't call function

Total noob, seriously and angrily struggling with Python...
What I'm trying to do SHOULD be simple:
Make a button.
Connect that button go a function.
Click button --> run function.
The problem comes when we have to use CLASS (which, no matter how much I read, study - or even pay to take classes continues to make zero sense to me)...
I've tried every concieveable combination of putting this little convert() function IN the class, of adding self.convert or root.convert - and NONE of it works. And, I am clueless why - or what to try next.
Here's the code:
from tkinter import *
from tkinter.ttk import Frame, Button, Style
def convert():
print("clicked")
kg = entry_kg.get()
print(kg)
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI() # initiate the GUI
# -------------------------------
def initUI(self):
self.master.title("Weight Converter")
self.pack(fill=BOTH, expand=True)
# -------------------------------------
frame_kg = Frame(self) # frame for Kilograms
frame_kg.pack(fill=X)
lbl_kg = Label(frame_kg, text="Kilograms", width=16)
lbl_kg.pack(side=LEFT, padx=5, pady=5)
entry_kg = Entry(frame_kg)
entry_kg.pack(fill=X, padx=(5, 30), expand=True)
# ------------------------------------------------
frame_btn = Frame(self) # frame for buttons
frame_btn.pack(fill=BOTH, expand=True, padx=20, pady=5)
btn_convert=Button(frame_btn, text="Convert", command=convert)
btn_convert.pack(side=LEFT, padx=5, pady=5)
# -------------------------------------------
def main():
root = Tk()
root.geometry("300x200+300+200")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
What am I doing wrong?
How to do it right?
The seemingly arbitrary and useless over-complication of a simple task is seriously maddening...
If you want your functions to be designed outside of the class, but want to call it from a button defined in the class, the solution is to create a method in your class which takes the input values and then passes them to the external function.
This is called decoupling. Your function is decoupled from the implementation of the UI, and it means that you are free to completely change the implementation of the UI without changing the function, and vice versa. It also means that you can reuse the same function in many different programs without modification.
The overall structure of your code should look something like this:
# this is the external function, which could be in the same
# file or imported from some other module
def convert(kg):
pounds = kg * 2.2046
return pounds
class Example(...):
def initUI(self):
...
self.entry_kg = Entry(...)
btn_convert=Button(..., command=self.do_convert)
...
def do_convert(self):
kg = float(self.entry_kg.get())
result = convert(kg)
print("%d kg = %d lb" % (kg, result))
Here's a modified version of your code that works. The changes have been indicated with ALL CAPS line comments. I obviously misunderstood your question (which does say you could figure out how to make the convert() function part of the class. However, you mentioned you wanted the opposite of that, so I'm modified the code here accordingly.
Essentially the problem boils down to the convert() function needing to access a tkinter.Entry widget that's created somewhere else—inside your Example class in this case.
One way of doing that would be to save the widget in a global variable and access it through the variable name assigned to it. Many folks do that because it's easiest to thing to do, but as you should know, global variables are considered a bad practice and are generally something to be avoided.
There's a common way to avoid needing one with tkinter, which is sometimes called "The extra arguments trick". To use it all you need to do is create a short anonymous function with an argument that has a default value defined—which in this case will be the Entry widget you want passed to the now "wrapped" convert() function. This can be done using what's called a lambda expression. The revised code below and the comments in it show and describe how to do this:
from tkinter import *
from tkinter.ttk import Frame, Button, Style
def convert(entry_widget): # ADDED WIDGET ARGUMENT
""" Some function outside class. """
print("clicked")
kg = entry_widget.get() # REFERENCE ENTRY WIDGET PASSED AS ARGUMENT
print(kg)
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI() # initiate the GUI
def initUI(self):
self.master.title("Weight Converter")
self.pack(fill=BOTH, expand=True)
frame_kg = Frame(self) # frame for Kilograms
frame_kg.pack(fill=X)
lbl_kg = Label(frame_kg, text="Kilograms", width=16)
lbl_kg.pack(side=LEFT, padx=5, pady=5)
entry_kg = Entry(frame_kg)
entry_kg.pack(fill=X, padx=(5, 30), expand=True)
frame_btn = Frame(self) # frame for buttons
frame_btn.pack(fill=BOTH, expand=True, padx=20, pady=5)
btn_convert=Button(frame_btn, text="Convert",
# DEFINE ANONYMOUS FUNCTION WITH DEFAULT ARGUMENT SO IT'S
# AUTOMATICALLY PASSED TO THE TARGET FUNCTION.
command=lambda entry_obj=entry_kg: convert(entry_obj))
btn_convert.pack(side=LEFT, padx=5, pady=5)
def main():
root = Tk()
root.geometry("300x200+300+200")
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
Your entry_kg is not known anywhere outside the scope of initUI method. That is why. You could convert it from a method variable to instance attribute to be within reach for class methods by replacing:
entry_kg = Entry(frame_kg)
entry_kg.pack(fill=X, padx=(5, 30), expand=True)
with:
self.entry_kg = Entry(frame_kg)
self.entry_kg.pack(fill=X, padx=(5, 30), expand=True)
Only then:
You can mention it in a class method like:
...
kg = self.entry_kg.get()
That way you if you make your convert a method under Example again:
def initUI(self):
...
def convert(self): # is defined under the same scope as initUI
print("clicked")
kg = self.entry_kg.get() # notice that it now refers to self.e...
print(kg)
also don't forget to replace command option as well:
btn_convert=Button(..., command=self.convert)
Or only then:
When outside the class scope, by using dot notation on the object that the class creates:
def main():
root = Tk()
root.geometry("300x200+300+200")
app = Example()
kg = app.entry_kg.get() # This would return an empty string, but it would still be valid
root.mainloop()
to be used with global methods, such as the current state of convert you need to make app (the object it is an attribute of) global, or pass it explicitly to the method.

Tkinter - widget with 'grid_remove' built in as part of class

I am doing some experimentation with tkinter and have run into a bit of trouble with grid_remove. I can use it fine with a simple button that links to a command that removes a specific widget, but I can't seem to get it to work when it is part of a class.
When I try and run this:
class Text(object):
def __init__(self, label_text, r, c):
self.label_text = label_text
self.r = r
self.c = c
self.label = Label(root, text = self.label_text).grid(row = self.r, column = self.c)
def hide(self):
self.grid_remove()
def show(self):
self.grid()
I get the error:
AttributeError: 'Text' object has no attribute 'grid_remove'
I also want to have a button controlling the visibility of the widgets, so how should I specify a command for the button? At the moment I have:
button = Button(root, text = 'Hide', command = one.hide()).grid(row = 2)
So, for others who have come across this problem, here's what I needed to change in order to get my script to work.
First of all, writing .grid() right after creating the Label was assigning the value of grid to self.label instead of assigning Label to it. The value of grid is a value of none so that was creating the first error. After fixing that part of the code it looks like:
self.label = Label(root, text = self.label_text)
self.label.grid(row = self.r, column = self.c)
The next problem was defining the hide and show functions. I was trying to grid_remove the whole Text class. However, the Text class comprises of many different things, one of which is a Label. I needed to specify to apply grid_remove to only the Label instead of the whole class. After fixing the definitions they look like this:
def hide(self):
self.label.grid_remove()
def show(self):
self.label.grid()
And the last error was the command in the buttons. I had written command = one.hide(). However, for some reason that is not known to me, I instead had to write only command = one.hide without the parentheses. After fixing that the buttons look like:
button = Button(root, text = 'Hide', command = one.hide).grid(row = 2)
So the reason my script wasn't working was not due to one simple error but a combination of all of these. I hope this will help someone else in the future!

Destroying widgets from a different subroutine in tkinter

So I'm using .place to set the location of my widgets at the moment.
def printresults():
SClabelspare=Label(cwindow, text ="Please enter the Customers ID Number:" ).place(x=10,y=560)
I'm looking to call another subroutine that will destroy these widgets. I believe there is something called .destroy() or .place_destroy? I'm not quite sure how these would work though and I have tried to create one that looked like this:
def destroy_widgets():
SClabelspare.destroy()
but it just produces an error code that says NameError: global name 'SClabelspare' is not defined
Any help will be appreciated!
First, place() returns None so SClabelspare==None not a Tkinter ID. Second it is local, so is garbage collected when the function exits. You have to keep a reference to the object which can be done in many ways. A Python tutorial would be a good idea to get the basics before you go further https://wiki.python.org/moin/BeginnersGuide/Programmers Also, programming a Tkinter app without using class structures is a frustrating experience, unless it is something very simple. Otherwise you get errors like yours and have to spend much time and effort trying to overcome them. This is an example that I already have and is meant to to give a general idea of the process.
from Tkinter import *
from functools import partial
class ButtonsTest:
def __init__(self):
self.top = Tk()
self.top.title("Click a button to remove")
Label(self.top, text="Click a button to remove it",
bg="lightyellow").grid(row=0)
self.top_frame = Frame(self.top, width =400, height=400)
self.button_dic = {}
self.buttons()
self.top_frame.grid(row=1, column=0)
Button(self.top_frame, text='Exit', bg="orange",
command=self.top.quit).grid(row=10,column=0, columnspan=5)
self.top.mainloop()
##-------------------------------------------------------------------
def buttons(self):
b_row=1
b_col=0
for but_num in range(1, 11):
## create a button and send the button's number to
## self.cb_handler when the button is pressed
b = Button(self.top_frame, text = str(but_num),
command=partial(self.cb_handler, but_num))
b.grid(row=b_row, column=b_col)
## dictionary key=button number --> button instance
self.button_dic[but_num] = b
b_col += 1
if b_col > 4:
b_col = 0
b_row += 1
##----------------------------------------------------------------
def cb_handler( self, cb_number ):
print "\ncb_handler", cb_number
self.button_dic[cb_number].grid_forget()
##===================================================================
BT=ButtonsTest()
Or, if this is supposed to be very simple, without a lot of hard-to-manage global variables, and if class structures would only introduce needless complexity, you might try something like this (it worked for me in the python3 interpreter from the command line):
from tkinter import *
root = Tk()
def victim():
global vic
vic = Toplevel(root)
vicblab = Label(vic, text='Please bump me off')
vicblab.grid()
def bumper():
global vic
bump = Toplevel(root)
bumpbutt = Button(bump, text='Bump off', command=vic.destroy)
bumpbutt.grid()
victim()
bumper()

Python Tkinter: adding a scrollbar to frame within a canvas

I am quite new to Tkinter, but, nevertheless, I was asked to "create" a simple form where user could provide info about the status of their work (this is sort of a side project to my usual work).
Since I need to have quite a big number of text widget (where users are required to provide comments about status of documentation, or open issues and so far), I would like to have something "scrollable" (along the y-axis).
I browsed around looking for solutions and after some trial and error I found something that works quite fine.
Basically I create a canvas, and inside a canvas a have a scrollbar and a frame. Within the frame I have all the widgets that I need.
This is a snipet of the code (with just some of the actual widgets, in particular the text ones):
from Tkinter import *
## My frame for form
class simpleform_ap(Tk):
# constructor
def __init__(self,parent):
Tk.__init__(self,parent)
self.parent = parent
self.initialize()
#
def initialize(self):
#
self.grid_columnconfigure(0,weight=1)
self.grid_rowconfigure(0,weight=1)
#
self.canvas=Canvas(self.parent)
self.canvas.grid(row=0,column=0,sticky='nsew')
#
self.yscrollbar = Scrollbar(self,orient=VERTICAL)
self.yscrollbar.grid(column =4, sticky="ns")
#
self.yscrollbar.config(command=self.canvas.yview)
self.yscrollbar.pack(size=RIGTH,expand=FALSE)
#
self.canvas.config(yscrollcommand=self.yscrollbar.set)
self.canvas.pack(side=LEFT,expand=TRUE,fill=BOTH)
#
self.frame1 = Frame(self.canvas)
self.canvas.create_window(0,0,window=self.frame1,anchor='nw')
# Various Widget
# Block Part
# Label
self.labelVariableIP = StringVar() # Label variable
labelIP=Label(self.frame1,textvariable=self.labelVariableIP,
anchor="w",
fg="Black")
labelIP.grid(column=0,row=0,columnspan=1,sticky='EW')
self.labelVariableIP.set(u"IP: ")
# Entry: Single line of text!!!!
self.entryVariableIP =StringVar() # variable for entry field
self.entryIP =Entry(self.frame1,
textvariable=self.entryVariableIP,bg="White")
self.entryIP.grid(column = 1, row= 0, sticky='EW')
self.entryVariableIP.set(u"IP")
# Update Button or Enter
button1=Button(self.frame1, text=u"Update",
command=self.OnButtonClickIP)
button1.grid(column=2, row=0)
self.entryIP.bind("<Return>", self.OnPressEnterIP)
#...
# Other widget here
#
# Some Text
# Label
self.labelVariableText = StringVar() # Label variable
labelText=Label(self.frame1,textvariable=
self.labelVariableText,
anchor="nw",
fg="Black")
labelText.grid(column=0,row=curr_row,columnspan=1,sticky='EW')
self.labelVariableTexta.set(u"Insert some texts: ")
# Text
textState = TRUE
self.TextVar=StringVar()
self.mytext=Text(self.frame1,state=textState,
height = text_height, width = 10,
fg="black",bg="white")
#
self.mytext.grid(column=1, row=curr_row+4, columnspan=2, sticky='EW')
self.mytext.insert('1.0',"Insert your text")
#
# other text widget here
#
self.update()
self.geometry(self.geometry() )
self.frame1.update_idletasks()
self.canvas.config(scrollregion=(0,0,
self.frame1.winfo_width(),
self.frame1.winfo_height()))
#
def release_block(argv):
# Create Form
form = simpleform_ap(None)
form.title('Release Information')
#
form.mainloop()
#
if __name__ == "__main__":
release_block(sys.argv)
As I mentioned before, this scripts quite does the work, even if, it has a couple of small issue that are not "fundamental" but a little annoying.
When I launch it I got this (sorry for the bad screen-capture):
enter image description here
As it can be seen, it only shows up the first "column" of the grid, while I would like to have all them (in my case they should be 4)
To see all of the fields, I have to resize manually (with the mouse) the window.
What I would like to have is something like this (all 4 columns are there):
enter image description here
Moreover, the scrollbar does not extend all over the form, but it is just on the low, right corner of the windows.
While the latter issue (scrollbar) I can leave with it, the first one is a little more important, since I would like to have the final user to have a "picture" of what they should do without needing to resize the windows.
Does any have any idea on how I should proceed with this?
What am I missing?
Thanks in advance for your help
In the __init__ method of your class, you do not appear to have set the size of your main window. You should do that, or it will just set the window to a default size, which will only show whatever it can, and in your case, only 1 column. Therefore, in the __init__ method, try putting self.geometry(str(your_width) + "x" + str(your_height)) where your_width and your_height are whatever integers you choose that allow you to see what you need to in the window.
As for your scrollbar issue, all I had to do was change the way your scrollbar was added to the canvas to a .pack() and added the attributes fill = 'y' and side = RIGHT to it, like so:
self.yscrollbar.pack(side = 'right', fill = 'y')
Also, you don't need:
self.yscrollbar.config(command=self.canvas.yview)
self.yscrollbar.pack(size=RIGHT,expand=FALSE)
Just add the command option to the creation of the scrollbar, like so:
self.scrollbar = Scrollbar(self,orient=VERTICAL,command=self.canvas.yview)
In all, the following changes should make your code work as expected:
Add:
def __init__(self,parent):
Tk.__init__(self,parent)
self.parent = parent
self.initialize()
# Resize the window from the default size to make your widgets fit. Experiment to see what is best for you.
your_width = # An integer of your choosing
your_height = # An integer of your choosing
self.geometry(str(your_width) + "x" + str(your_height))
Add and Edit:
# Add `command=self.canvas.yview`
self.yscrollbar = Scrollbar(self,orient=VERTICAL,command=self.canvas.yview)
# Use `.pack` instead of `.grid`
self.yscrollbar.pack(side = 'right', fill = 'y')
Remove:
self.yscrollbar.config(command=self.canvas.yview)
self.yscrollbar.pack(size=RIGHT,expand=FALSE)

Create a simple GUI for a minimalistic python script

I wrote a small python function, which takes several numerical input parameters and prints many lines with statements, which going to be used in an experiment, like this toy example:
def myADD(x,y,z):
res = x + y + z
print("the result is: {0}+{1}+{2}={3}").format(x,y,z,res)
I would like to create a minimalistic GUI, simply an overlay which calls my myADD.py script, where I can fill those parameters x,y,z and after clicking a "compute" button a text field occurs with the print statement.
Does anyone has a template, I was looking into the TKinter, but my attempts by manipulating other templates didn't succeed.
Would appreciate help, thanks.
Tkinter is a fantastic choice since it is built-in. It is ideally suited for this type of quick, minimalistic GUI.
Here's a basic framework for a Tkinter app to show you how simple it can be. All you need to do is add your function, either by importing it or including it in the same file:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.entry = {}
# the basic layout is a form on the top, and
# a submit button on the bottom
form = tk.Frame(self)
submit = tk.Button(self, text="Compute", command=self.submit)
form.pack(side="top", fill="both", expand=True)
submit.pack(side="bottom")
# this fills in the form with input widgets for each parameter
for row, item in enumerate(("x", "y", "z")):
label = tk.Label(form, text="%s:"%item, anchor="w")
entry = tk.Entry(form)
label.grid(row=row, column=0, sticky="ew")
entry.grid(row=row, column=1, sticky="ew")
self.entry[item] = entry
# this makes sure the column with the entry widgets
# gets all the extra space when the window is resized
form.grid_columnconfigure(1, weight=1)
def submit(self):
'''Get the values out of the widgets and call the function'''
x = self.entry["x"].get()
y = self.entry["y"].get()
z = self.entry["z"].get()
print "x:", x, "y:", y, "z:", z
if __name__ == "__main__":
# create a root window
root = tk.Tk()
# add our example to the root window
example = Example(root)
example.pack(fill="both", expand=True)
# start the event loop
root.mainloop()
If you want the result to appear in the window, you can create another instance of a Label widget, and change it's value when you perform the computation by doing something like self.results_label.configure(text="the result")
Tkinter is usually a good start because it is bundled with Python (tutorial).
That said, Tk is pretty old and therefore "odd" at times. If you want a more modern UI, have a look at PyQt. It's based on Qt but it doesn't come with Python by default, so you have to install it manually.

Categories