Calling Functions within Classes from Tkinter Buttons - python

I am currently trying to use classes within my code which I have not used before. My goal is to When the power on button is pressed the system the system checks whether the power is on or not and if it is no it returns "Power On" and the same for Power Off and so on. I am currently struggling when the button is pressed I am unsure how to send a command to the function within control with the specified text e.g "PWR"
Below is my code any help is much appreciated thank you
import tkinter as tk
window = tk.Tk()
class Power:
def __init__(self, x):
if x == 0:
# Run def control
# Change function in def control to Power
print ("Power On")
if x == 1:
# Run def control
# Change function in def control to Power
print("Power Off")
if x == 2:
# Run def Control
# Change Function to Inp
# Add which input has been selected to the end of the return
print ("Input 1")
if x == 3:
# Run def Control
# Change Function to Inp
# Add which input has been selected to the end of the return
print ("Input 2")
def control(self, function):
if function[:3] == "PWR":
if "on" in function.lower():
return ("Power On")
elif "off" in function.lower():
return("Power Off")
else:
return ("Power Error")
elif function[:3] == 'INP':
inp = function[3:]
return 'Input ' + inp
CtrlBtnsTxt = ["Power On", "Power Off", "Input 1", "Input 2"]
for i in range (4):
CtrlBtns = tk.Button(window, width = 10, height = 5, text = CtrlBtnsTxt[i], command = lambda x = i: Power (x) )
CtrlBtns.grid (row = i, column = 0)
window.mainloop()

It will be better for the class instance to persist across commands. This way you could simply memorize the current button states and use that to adjust the behavior during the next call :
import tkinter as tk
window = tk.Tk()
class Power:
# ====> Memorizes states
power = False
curInput = 0
# ====> Replace __init__ by a method you can call without creating a new instance
def command(self, x):
if x == 0:
print (self.control("PWR on"))
if x == 1:
print (self.control("PWR off"))
if x == 2:
print (self.control("INP 1"))
if x == 3:
print (self.control("INP 2"))
def control(self, function):
if function[:3] == "PWR":
# ====> Includes the stored state in the tests and stores the new state
if "on" in function.lower() and self.power == False:
self.power = True
return "Power On"
# ====> Idem
elif "off" in function.lower() and self.power == True
self.power = False
return "Power Off"
else:
return ("Power Error")
elif function[:3] == 'INP':
inp = function[3:]
# ====> Optionally you can do the same for the input
if (inp == self.curInput): return 'Input already set'
self.curInput = inp
return 'Input ' + inp
CtrlBtnsTxt = ["Power On", "Power Off", "Input 1", "Input 2"]
# ====> Creates a unique instance of the class and uses it below
power = Power()
for i in range (4):
CtrlBtns = tk.Button(window, width = 10, height = 5, text = CtrlBtnsTxt[i], command = lambda x = i: power.command(x) )
CtrlBtns.grid (row = i, column = 0)
window.mainloop()

Related

Python tkinter - Change the Label created by a loop, how can I do?

I read a lot for it and I have the sentiment that there is no solution for me.
Context: user has to enter 6-digit calculation number in the entries. The 9x entries are created by a loop. I wrote the condition to detect if the user did a mistake : number which contains less or more than 6 digit or / and contains a letter. They are working, I tested them. Only 6-digit number are accepted (if 0, it's ok since it means that the user just need to get less than 9x documents).
Goal: if the user did one mistake, a message "Error in the number" at the place of the "0" has to appear right next to the concerned and "faulty" entry.
Everything will be activated through a button.
What I tried: with a list, but it doesn't work.
How can I change the Label dynamically created by the loop ?
user_entries=[]
error_list_length=[0,0,0,0,0,0,0,0,0]
error_list_letters=[0,0,0,0,0,0,0,0,0]
error_calculation_list=[0,0,0,0,0,0,0,0,0]
nbofcalc=9
a=0
b=1
ddd=791250
#------ Function to check if the entered calculation number are composed of 6 characters (mandatory)
def check_calc_number():
global gg_error
gg=0
gg_error=0
while gg<nbofcalc:
if len(user_entries[gg].get()) != 6:
if len(user_entries[gg].get()) ==0:
gg_error+=0
error_list_length[gg]=0
else:
gg_error+=1
error_list_length[gg]=1
else:
gg_error+=0
error_list_length[gg]=0
gg+=1
#------ Function to check if the entered calculation number contains a or many letters (prohibited)
def check_calc_letter():
global hh_error
hh=0
hh_error=0
alphabet_x='a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for x in range(nbofcalc):
for n in user_entries[hh].get():
if n in alphabet_x:
hh_error+=1
error_list_letters[hh]=1
hh+=1
#------ Function to check with entries has an error and update the list "error_calculation_list" to try to displays the message "Error in the number" next to the concerned entries
def error_length_letters_display():
ww=0
while ww<nbofcalc:
if error_list_length[ww]==1 :
error_calculation_list[ww]="Error"
var_calculation.set(error_calculation_list[ww])
if error_list_letters[ww]==1:
error_calculation_list[ww]="Error"
var_calculation.set(error_calculation_list[ww])
ww+=1
#----- Loop to crate the entries and Label
for x in range(nbofcalc):
cadre1=Frame(fenetre)
cadre1.pack(side=TOP,anchor=NW)
cadre=Frame(cadre1)
cadre.pack()
c=str(b)
calc=Label(cadre,text="Calculation "+c+" ")
calc.pack(side=LEFT)
var_entry=StringVar()
my_entry=Entry(cadre,textvariable=var_entry, bd=5)
my_entry.insert(0,ddd)
my_entry.pack(side=LEFT)
var_calculation=StringVar()
var_calculation.set(error_calculation_list[a])
calc_error_frame=Label(cadre, textvariable=var_calculation) # The Label to change if error
calc_error_frame.pack(side=RIGHT)
user_entries.append(my_entry)
a+=1
b+=1
ddd+=1
Thank you !
You can store values and objects you need to change, so then you can change them dinamically. See this example
from tkinter import *
r = Tk()
my_entries = []
for i in range(5):
e = Label(r, text=i)
my_entries.append(e)
e.pack(side='top')
r.after(4000, lambda: my_entries[2].configure(text='Example'))
r.mainloop()
EDIT 1:As TheLizzard and Cool Cloud pointed out, it's better to avoid using time.sleep() while using tkinter. Replaced with non-blocking after()
you could setup the trace function for tk.StringVar(), when user enter any value into Entry, it will be checked. for example as shown on below user only can type decimal, and you and setup length as well.
def create_widgets(self):
self.vars = tk.StringVar()
self.vars.trace('w', partial(self.validate2, 1, 1))
# Min Voltage Validating Part
self.vars1 = tk.StringVar()
self.vars1.trace('w', partial(self.validate2, 2, 2))
# Max Voltage Validating Part
self.vars2 = tk.StringVar()
self.vars2.trace('w', partial(self.validate2, 3, 4))
# Current Validating Part
self.vars3 = tk.StringVar()
self.vars3.trace('w', partial(self.validate2, 4, 3))
# Channel Validating Part
# function( key, size)
self.enter_info = tk.Label(self, text="Please enter your information: ", bg="lightgrey")
self.enter_info.grid(tke_Table_EnterInfo)
self.voltage = tk.Label(self)
self.voltage["text"] = "MinVoltage"
self.voltage.grid(tke_Label_MinVoltage)
self.voltageInput = tk.Entry(self, width=10, textvariable=self.vars).grid(tke_StringBox_MinVoltage)
self.vars.set(0)
# Min Voltage Validating Part
self.current = tk.Label(self)
self.current["text"] = "MaxVoltage"
self.current.grid(tke_Label_MaxVoltage)
self.currentInput = tk.Entry(self, width=10, textvariable=self.vars1).grid(tke_StringBox_MaxVoltage)
self.vars1.set(5)
# Max Voltage Validating Part
self.power = tk.Label(self)
self.power["text"] = "Current"
self.power.grid(tke_Label_MaxCurrent)
self.powerInput = tk.Entry(self, width=10, textvariable=self.vars2).grid(tke_StringBox_MaxCurrent)
self.vars2.set(62.5)
# Max Current Validating Part
self.channel = tk.Label(self)
self.channel["text"] = "channel"
self.channel.grid(tke_Label_Channel)
self.channelInput = tk.Entry(self, width=10, textvariable=self.vars3).grid(tke_StringBox_Channel)
self.vars3.set(8)
# Max Channel Validating Part
def validate2(self, key, size, *args):
# TODO:add more information
if key == 1:
value = self.vars.get()
elif key == 2:
value = self.vars1.get()
elif key == 3:
value = self.vars2.get()
else:
value = self.vars3.get()
if not value.isdecimal():
print(len(value))
# if len(value) < 2:
corrected = ''.join(filter(str.isdecimal, value))
if key == 1:
self.vars.set(corrected)
elif key == 2:
self.vars1.set(corrected)
elif key == 3:
self.vars2.set(corrected)
else:
self.vars3.set(corrected)
if key == 1:
corrected = self.vars.get()
corrected = corrected[0:size]
self.vars.set(corrected)
elif key == 2:
corrected = self.vars1.get()
corrected = corrected[0:size]
self.vars1.set(corrected)
elif key == 3:
corrected = self.vars2.get()
corrected = corrected[0:size]
self.vars2.set(corrected)
else:
corrected = self.vars3.get()
corrected = corrected[0:size]
self.vars3.set(corrected)

Connect a python script with Tkinter

I want to design a Gui script for a NameSort script.
I want to develop a python3 script with Gui. Here is my code for Cli only
text = open('/Users/test/Desktop/readme.txt','r')
def readtxt(txt): #turns txt to ls
dictls=(txt.read()).splitlines()
return dictls
def getdict(dictls): #turns to dictionary
dict1 = {dictls.index(i) : i for i in dictls}
return dict1
def getkey(diction,index): #getkey
return diction[index]
def randomord(x,z): #random order generator, won't generate repeditive numbers
import random
output = []
done = []
y = 0 #already generated
while y <= x:
rnum = random.randint(0,z)
if rnum not in done:
output.append(rnum)
done.append(rnum)
y+=1
return output
def main():
ls=readtxt(text)
while True:
print(f'\nThere are {len(ls)} names on the list.')
try:
h = int(input('Number of names to gen: '))
if h-1 <= len(ls)-1:
outls = [getkey(getdict(ls),i) for i in randomord(h-1,len(ls)-1)]
print('\n'.join(outls))
else:
print(f'[*]ERR: There are only {len(ls)} names on the list.')
except:
print('[*]ERR')
main()
Now I have tried these code
text = open('/root/Desktop/Python/gui/hell.txt','r')
def readtxt(txt): #turns txt to ls
dictls=(txt.read()).splitlines()
return dictls
def getdict(dictls): #turns to dictionary
dict1 = {dictls.index(i) : i for i in dictls}
return dict1
def getkey(diction,index): #getkey
return diction[index]
def randomord(x,z): #random order generator, won't generate repeditive numbers
import random
output = []
done = []
y = 0 #already generated
while y <= x:
rnum = random.randint(0,z)
if rnum not in done:
output.append(rnum)
done.append(rnum)
y+=1
return output
def main():
ls=readtxt(text)
while True:
print(f'\nThere are {len(ls)} names on the list.')
try:
h = int(input('Number of names to gen: '))
if h-1 <= len(ls)-1:
outls = [getkey(getdict(ls),i) for i in randomord(h-1,len(ls)-1)]
print('\n'.join(outls))
else:
print(f'[*]ERR: There are only {len(ls)} names on the list.')
except:
print('[*]ERR')
global startmain
startmain = 0
def test1():
startmain = 1
###########################
##########################
###################################
from tkinter import *
import tkinter as tk
window = tk.Tk()
p1 = tk.Label(
text="Thomas-name-sort",
fg="red",
bg='green',
width=10,
height=8
)
name_ent = tk.Label(text="输入生成数量Number of names to gen:")
entry = tk.Entry()
name_ent.pack()
entry.pack()
name = entry.get()
b1 = tk.Button(
text="auto-testing自检",
bg="blue",
fg="orange",
width=20,
height=5,
command=test1()
)
b1.pack()
if startmain == 1:
main()
#bind-zone
window.mainloop()
It does not work.
I want to design a Gui script for a NameSort script.
I could not bind the button with the function
T have tried "command=main()" and "button.bind("", func=main()"
Please help me!!!
To set a button's function in tkinter, you only pass the function itself in the command parameter i.e.
command = test1,
Not command = test1() as in this case you are just giving it the result of the function (which is None). This is so that it can execute the function each time the button is pressed.
If you want to provide in arguments for it to execute, you'll then have to use a lambda function:
command = lambda: test1(param1, param2),
You can find decent guides on buttons in tkinter here if you're interested

Tkinter/Python hanging on user response if response is the last option in a while or if loop

The following code works for requesting input from a user through the Tkinter GUI and turning that input into a usable variable in the main script. However, any value that I put as the last in a list in the if statement (here "4") will hang and crash the program upon enter. This was also the case for "n" in a yes/no scenario. It also happens if I replace the if statement with a while not in [values] - the final value will crash the program. Is this just a quirk of Tkinter or is there something that I am missing?
import tkinter as tk
from tkinter import *
# get choice back from user
global result
badinput = True
while badinput == True:
boxwidth = 1
result = getinput(boxwidth).strip().lower()
if result in ['1', '2', '3', '4']:
badinput = False
# iterate through play options
if result == '1':
# Do Something
elif result =='2':
# Do Something
elif result =='3':
# Do Something
else:
# Do Something
def getinput(boxwidth):
# declaring string variable for storing user input
answer_var = tk.StringVar()
# defining a function that will
# get the answer and set it
def user_response(event):
answer_var.set(answer_entry.get())
return
answer_entry = tk.Entry(root, width = boxwidth, borderwidth = 5)
# making it so that enter calls function
answer_entry.bind('<Return>', user_response)
# placing the entry
answer_entry.pack()
answer_entry.focus()
answer_entry.wait_variable(answer_var)
answer_entry.destroy()
return answer_var.get()
In case anyone is following this question, I did end up solving my problem with a simple if statement within the callback. I can feed a dynamic "choicelist" of acceptable responses into the callback upon user return. If the answer is validated, the gate_var triggers the wait function and sends the program and user response back into the program.
'''
def getinput(boxwidth, choicelist):
# declaring string variable for storing user input
answer_var = tk.StringVar()
gate_var = tk.StringVar()
dumplist = []
# defining a function that will
# get the answer and set it
def user_response(event):
answer_var.set(answer_entry.get())
if choicelist == None:
clearscreen(dumplist)
gate_var.set(answer_entry.get())
return
if answer_var.get() in choicelist:
# passes a validated entry on to gate variable
clearscreen(dumplist)
gate_var.set(answer_entry.get())
else:
# return to entry function and waits if invalid entry
clearscreen(dumplist)
ErrorLabel = tk.Label(root, text = "That is not a valid response.")
ErrorLabel.pack()
ErrorLabel.config(font = ('verdana', 18), bg ='#BE9CCA')
dumplist.append(ErrorLabel)
return
global topentry
if topentry == True:
answer_entry = tk.Entry(top, width = boxwidth, borderwidth = 5)
else:
answer_entry = tk.Entry(root, width = boxwidth, borderwidth = 5)
# making it so that enter calls function
answer_entry.bind('<Return>', user_response)
# placing the entry
answer_entry.pack()
answer_entry.focus()
answer_entry.wait_variable(gate_var)
answer_entry.destroy()
return answer_var.get()
'''

validating a text entry on tkinter

I'm creating a program that allows the user to select a category and enter a value to calculate a charge. I would like to validate the text entry using my own validation file. However, when I run the program and enter nothing in the text entry, the error window keeps popping up over and over again. In addition, when I run the program and enter a valid number in the entry field, the charge comes out to 0.0, even though I have defined the calculation for the total charge.
Here is the program:
import tkinter
import tkinter.messagebox
import ValidationFile
validationObject = ValidationFile.ValidationClass ()
class MyGUI:
def __init__ (self):
self.main_window = tkinter.Tk ()
self.top_frame = tkinter.Frame (self.main_window)
self.middle_frame = tkinter.Frame (self.main_window)
self.bottom_frame = tkinter.Frame (self.main_window)
self.phone_var = tkinter.IntVar ()
self.phone_var.set (1)
self.pb1 = tkinter.Radiobutton (self.top_frame, \
text = 'Daytime (6:00 AM - 5:59 PM)', variable = self.phone_var, \
value = 0.12)
self.pb2 = tkinter.Radiobutton (self.top_frame, \
text = 'Evening (6:00 PM - 11:59 PM)', variable = self.phone_var, \
value = 0.07)
self.pb3 = tkinter.Radiobutton (self.top_frame, \
text = 'Off-Peak (Midnight - 5:50 AM)', variable = self.phone_var, \
value = 0.05)
#pack phone buttons
self.pb1.pack ()
self.pb2.pack ()
self.pb3.pack ()
#create input and output buttons
self.txtInput = tkinter.Entry (self.middle_frame, \
width = 10)
self.value = tkinter.StringVar ()
self.lblOutput = tkinter.Label (self.middle_frame, \
width = 10, textvariable = self.value)
self.txtInput.pack()
self.lblOutput.pack ()
#create OK buton and QUIT button
self.ok_button = tkinter.Button (self.bottom_frame, \
text = 'OK', command = self.show_choice)
self.quit_button = tkinter.Button (self.bottom_frame, \
text = 'QUIT', command = self.main_window.destroy)
#pack the buttons
self.ok_button.pack (side = 'left')
self.quit_button.pack (side = 'left')
#pack the frames
self.top_frame.pack ()
self.middle_frame.pack ()
self.bottom_frame.pack ()
#start the mainloop
tkinter.mainloop ()
def show_choice (self):
choice = self.phone_var.get ()
value = -1
while value == -1:
valueEntry = self.txtInput.get()
if valueEntry == '':
value = -1
tkinter.messagebox.showinfo (title = 'ERROR', \
message = 'Please enter a valid number.')
else:
value = validationObject.checkFloat (valueEntry)
total = choice * value
self.value.set (total)
#create instance of MyGUI class
my_GUI = MyGUI ()
Here is the validation file:
#create validation class
class ValidationClass:
def checkFloat (self, inputString):
try:
result = float (inputString)
except Exception:
return -1
if result < 0:
return -1
else:
return result
def checkInteger (self, inputString):
try:
result = int (inputString)
except Exception:
return -1
if result < 0:
return -1
else:
return result
You made an infinite loop with while value == -1:. Nowhere in that loop do you pause to allow the user to try again. You don't need the loop at all:
def show_choice (self):
valueEntry = self.txtInput.get()
value = validationObject.checkFloat(valueEntry)
if value == -1:
tkinter.messagebox.showinfo (title = 'ERROR', \
message = 'Please enter a valid number.')
else:
choice = self.phone_var.get()
total = choice * value
self.value.set (total)
Once you fix that you will have another problem: you use float values in your options but the variable is an IntVar, which can only handle integers. So "choice" will always be 0. You need to use DoubleVar instead.

raw_input and timeout [duplicate]

This question already has answers here:
Keyboard input with timeout?
(28 answers)
Closed 8 years ago.
I want to do a raw_input('Enter something: .'). I want it to sleep for 3 seconds and if there's no input, then cancel the prompt and run the rest of the code. Then the code loops and implements the raw_input again. I also want it to break if the user inputs something like 'q'.
There's an easy solution that doesn't use threads (at least not explicitly): use select to know when there's something to be read from stdin:
import sys
from select import select
timeout = 10
print "Enter something:",
rlist, _, _ = select([sys.stdin], [], [], timeout)
if rlist:
s = sys.stdin.readline()
print s
else:
print "No input. Moving on..."
Edit[0]: apparently this won't work on Windows, since the underlying implementation of select() requires a socket, and sys.stdin isn't. Thanks for the heads-up, #Fookatchu.
If you're working on Windows you can try the following:
import sys, time, msvcrt
def readInput( caption, default, timeout = 5):
start_time = time.time()
sys.stdout.write('%s(%s):'%(caption, default));
input = ''
while True:
if msvcrt.kbhit():
chr = msvcrt.getche()
if ord(chr) == 13: # enter_key
break
elif ord(chr) >= 32: #space_char
input += chr
if len(input) == 0 and (time.time() - start_time) > timeout:
break
print '' # needed to move to next line
if len(input) > 0:
return input
else:
return default
# and some examples of usage
ans = readInput('Please type a name', 'john')
print 'The name is %s' % ans
ans = readInput('Please enter a number', 10 )
print 'The number is %s' % ans
I have some code which makes a countdown app with a tkinter entry box and button so they can enter something and hit the button, if the timer runs out the tkinter window closes and tells them they ran out of time.
I think most other solutions to this problem don't have a window which pops up so thought id add to the list :)
with raw_input() or input(), it isn't possible as it stops at the input section, until it receives input, then it carries on...
I have taken some code from the following link:
Making a countdown timer with Python and Tkinter?
I used Brian Oakley's answer to this problem and added the entrybox etc.
import tkinter as tk
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
def well():
whatis = entrybox.get()
if whatis == "": # Here you can check for what the input should be, e.g. letters only etc.
print ("You didn't enter anything...")
else:
print ("AWESOME WORK DUDE")
app.destroy()
global label2
label2 = tk.Button(text = "quick, enter something and click here (the countdown timer is below)", command = well)
label2.pack()
entrybox = tk.Entry()
entrybox.pack()
self.label = tk.Label(self, text="", width=10)
self.label.pack()
self.remaining = 0
self.countdown(10)
def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
app.destroy()
print ("OUT OF TIME")
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
I know what I added was a bit lazy but it works and it is an example only
This code works for Windows with Pyscripter 3.3
For rbp's answer:
To account for input equal to a Carriage Return simply add a nested condition:
if rlist:
s = sys.stdin.readline()
print s
if s == '':
s = pycreatordefaultvalue

Categories