Clearing a list when tkinter button is clicked - python

I have a piece of code below, I have a button (prnt_button) that must empty a list and a text box, but unfortunately, it empties only the text box, how could I change the code, so that the button empties also the list?
# This is in a for loop, you press a button to go to the next step,
# every step you comes along the next piece of code:
if k == 3:
doc = DocxTemplate("template.docx")
context = { # Fill in some template variables
}
doc.render(context)
docin = f"{numlist[0]}_{numlist[1]}_{numlist[2]}"
locdoc = f'{text}\\' + docin
doc.save(f"{locdoc}.docx")
prntng_l.append(docin)
# and more things, which work fine.
k = 1
else:
k = k + 1
maken_files_not_printed = Text(mult_prnt_frame1, width=column_width, height=10, state='disabled')
maken_files_not_printed.grid(row=1, column=0)
def print_all_files_in_list(prntng_l):
maken_files_not_printed.config(state="normal")
maken_files_not_printed.delete(1.0, 'end')
maken_files_not_printed.config(state="disabled")
prnt_button.config(text="Print: 0", state='disabled')
prntng_l = []
return (prntng_l)
prnt_button = Button(mult_prnt_frame1, text="Print 0", width=column_width + 2, height=1,
command=lambda: print_all_files_in_list(prntng_l), state='normal')
prnt_button.grid(row=0, column=0)
maken_files_not_printed.config(state='normal')
maken_files_not_printed.delete(1.0, 'end')
for q in range(len(prntng_l)):
prnt_button.config(text="Print: " + str(q + 1), state="normal")
maken_files_not_printed.insert('end', "Naam bestand " + str(q + 1) + ":\n" + prntng_l[q] + "\n")
maken_files_not_printed.config(state='disabled')
Could someone please help me? Thanks in advance.

Related

tkinter GUI Text Box is not displaying all results similar to terminal output

I am trying to make a loan amortization table inside tkinter GUI using text box. However, all the loan amortization table is fully displayed in the terminal console as an output.
The tkinter text BOX output display's only the last line: Here is the full code:
import math
import tkinter as tk
from tkinter import *
def calcLoanPayment(p, r, n, t):
if p > 0 and r > 0 and n > 0 and t > 0:
clp = (p *(r/n)*(pow(1 + r/n, n * t)))/(pow(1 + r/n, n * t)-1)
clp = round(clp, 2)
return clp
else:
return -1
def runApp(): #This will bound the function runapp to the calcbtn.
print("running")
p = principalentry.get() #this will get value as a string
try:
p = float(p) #this will make cast it to float variable.
except:
p = -1
principalentry.delete(0, END) #this will clear the entry after calculation
r = interestrateentry.get()
try: #Try will help from crushing the program when wrong value entered
r = float(r)
except:
r = -1
interestrateentry.delete(0, END) #this will clear the entry after calculation
n = compoundintervalentry.get() #this will get value as a string
try:
n = int(n) #this will make cast it to float variable.
except:
n = -1
compoundintervalentry.delete(0, END) #this will clear the entry after calculation
t = durationentry.get()
try: #Try will help from crushing the program when wrong value entered
t = int(t)
except:
t = -1
durationentry.delete(0, END) #this will clear the entry after calculation
clp = calcLoanPayment(p,r,n,t)
print(clp)
paymentinterval = n*t
paymentinterval = int(paymentinterval)
startingBalance=p
endingBalance=p
for i in range(1, paymentinterval+1):
interestcharge =r/n*startingBalance
endingBalance=startingBalance+interestcharge-clp
result ="\t\t"+str(i)+"\t\t" +str(round (startingBalance, 1)
)+"\t\t"+str(round (interestcharge, 1))+"\t\t"+str(round (clp, 1)
)+"\t\t"+str(round (endingBalance, 1))
startingBalance = endingBalance
print(result)
finaloutput = result
output.config(state="normal")
output.delete(0.0, 'end')
output.insert(END, finaloutput)
output.config(state="disabled")
#Frontend maincode Startshere:
#sketch the design of the user interface first.
root = tk.Tk()
root.config(bg="#F8B135")
root.state("zoomed")
#There are 3 steps to build event programming
#Step 1: construct the widgets
#step 2: configure the widget to make it nicer
#srep 3: place or pack the widget in the root window
title = Label(root, text = "Loan Payment Calculator", font = 'bold')
title.config(fg='white', bg="#28348A")
title.pack(fill = BOTH)
principallabel = Label(root, text="Principal: ")
principallabel.config(anchor = W, font = 'bold', bg="#F8B135")
principallabel.place(x=50, y=30)
principalentry = Entry(root)
principalentry.config(bg="#ffffff")
principalentry.place(x=400, y=30)
interestratelabel = Label(root, text="Interest Rate: enter as Decimal (eg.2% as 0.02) ")
interestratelabel.config(anchor = W, font = 'bold', bg="#F8B135")
interestratelabel.place(x=50, y=70)
interestrateentry = Entry(root)
interestrateentry.config(bg="#ffffff")
interestrateentry.place(x=400, y=70)
compoundintervallabel = Label(root, text="Compound Interval: ")
compoundintervallabel.config(anchor = W, font = 'bold', bg="#F8B135")
compoundintervallabel.place(x=50, y=110)
compoundintervalentry = Entry(root)
compoundintervalentry.config(bg="#ffffff")
compoundintervalentry.place(x=400, y=110)
durationlabel = Label(root, text="Duration: ")
durationlabel.config(anchor = W, font = 'bold', bg="#F8B135")
durationlabel.place(x=50, y=150)
durationentry = Entry(root)
durationentry.config(bg="#ffffff")
durationentry.place(x=400, y= 150)
output = Text(root)
output.config(width= 150, height = 100, bg="white", state="disabled", borderwidth=2, relief= "groove", wrap='word')
output.place(x=50, y=230)
calcbtn = Button(root, text= "Calculate", font = 'bold', highlightbackground="#3E4149")
calcbtn.config(fg='#28348A',command=runApp)
calcbtn.place(x=50, y=190)
root. mainloop()
file.close()`
I couldn't figure out How to display the whole output like the terminal did on the tkinter textbox output function. your help is much appreciated.
There are 2 small changes to the code in the function runApp()
output.config(state="normal")
# output.delete(0.0, 'end') # This line deletes the last entry in the text box. Remove this
output.insert(END, finaloutput + '\n') # Add a newline here. Generates the desired 'tabular' output
output.config(state="disabled")

Tkinter entry.get() does not update [ Gurobi Python ]

For simpler demonstration of my problem I'm using a very simple Linear Program.
I want to set the right hand side of one constraint to a certain value that I type into an entry box (Tkinter).
When I click on the button, the variables that are not equal to zero will be shown in the label.
The problem: the initial value of the entry box is 0. When I change it to e.g. 100 I still get the same results. The results don't change whatever I type into the entry box.
When I hardcode the right hand side value of the constraint to any other value, the results change but not if I change it in my entry box.
It seems like whatever I type into my entry box, Python always takes the initial value, which was 0. It doesn't update to what I've typed in
import Tkinter as tk
from gurobipy import *
WIDTH = 330
HEIGHT = 500
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
conslabel = tk.Label(root, text='Constraint value')
conslabel.place(relx=0.1, rely = 0.02)
var = tk.IntVar()
entry = tk.Entry(root, textvariable=var)
entry.place(relx=0.5, rely=0.02, relwidth=0.3)
button = tk.Button(root, text = "yield result", command = lambda: [setEntryValue(), build_model(entryValue), m.optimize(), results(), m.reset()])
button.place(relx = 0.5, rely = 0.675, anchor = tk.CENTER)
outputLabel = tk.Label(root, bg='white')
outputLabel.place(relx = 0.5, rely = 0.85, anchor = tk.CENTER, relheight = 0.2, relwidth = 0.8)
entryValue = 0
def build_model(entryValue):
m = Model("lp1")
m.setParam('OutputFlag', False)
x1 = m.addVar(name="x1")
x2 = m.addVar(name="x2")
m.update()
m.setObjective(5*x1 + 4*x2, GRB.MAXIMIZE)
m.addConstr(6*x1 + 4*x2 <= 24, "constr_1")
m.addConstr(1*x1 + 2*x2 <= 6, "constr_2")
m.addConstr(-1*x1 + 1*x2 <= 1, "constr_3")
m.addConstr(x2 <= entryValue, "constr_4")
def setEntryValue():
global entryValue
entryValue = var.get()
return entryValue
def labeltext():
finalvars = ''
for v in m.getVars():
if v.x != 0:
finalvars += ' ' + str(v.varName)
return finalvars
def results():
for v in m.getVars():
print('%s: %f' % (v.varName, v.x))
print('Obj: %f' % m.objVal)
outputLabel['text'] = labeltext()
root.mainloop()
You are getting the value of the Entry widget about a millisecond after you create it, way before the user has a chance to enter anything. entryValue doesn't get updated when the user enters data.
You need to wait to call entry.get() until the user has a chance to enter data. Typically that means to do it in a callback associated with a button or menu item.
You should create a function for your "yield result" button instead of using lambda. Then, in that function you can set up your data and compute your result.
It should look something like this:
def yield_result():
entryValue = int(entry.get())
m.setObjective(5*x1 + 4*x2, GRB.MAXIMIZE)
m.addConstr(6*x1 + 4*x2 <= 24, "constr_1")
m.addConstr(1*x1 + 2*x2 <= 6, "constr_2")
m.addConstr(-1*x1 + 1*x2 <= 1, "constr_3")
m.addConstr(x2 <= entryValue, "constr_4")
return [m.optimize(), results()]
...
button = tk.Button(root, text = "yield result", command = yield_result)
I think the problem is the fact that you need to assign a textvariable to your Entry:
entry_var = tk.StringVar()
entry = tk.Entry(root, textvar = entry_var)
when you want to get the text in the entry box, you can just use string = entry_var.get()

How to display GUI message indicating which checkbutton(s) and radiobutton have been selected in tkinter in Python?

I am super new to Python and programming in general. I'm messing around in tkinter trying to make a silly and simple program so I can get more comfortable with it.
What I am doing with my program is asking for the user's name, age, if they remember certain events, and if they feel old or not. Like I said, it is supposed to be a light hearted program just to put my skills to the test.
Now I wonder, how to display GUI message indicating which checkbutton(s) and radiobutton have been selected in tkinter in Python?
Here is the code and a screenshot. I'll include a screenshot of my results but I can get the name and age to work like it should but can't get the message to appear correctly in the GUI with the checkboxes or radio button.
from tkinter import *
class MyFrame(Frame):
def __init__(self):
Frame.__init__(self)
self.master.geometry("600x400")
self.master.title("How to tell if you're old")
self.event = NONE
self.old = NONE
self.grid()
#user name and age
self.prompt = Label(self, text="What's your name?")
self.prompt.grid(row=0, column=0, pady=5)
self.input_name = Entry(self)
self.input_name.grid(row=0, column=1, pady=5)
self.prompt = Label(self, text="How old are you?")
self.prompt.grid(row=2, column=0, pady=5)
self.input_age = Entry(self)
self.input_age.grid(row=2, column=1, pady=10)
#user asks user if they remember certain events
self.prompt = Label(self, text="Which of these events do"
"you remember (may select more than one)?")
self.prompt.grid(row=3, columnspan=5, pady=5)
self.wheel_event = IntVar()
self.check_wheel_event = Checkbutton(self, text="Invention of the wheel",
variable=self.wheel_event, command=self.set_response_event)
self.check_wheel_event.grid(row=4, column=0, padx=5)
self.firstFlight_event = IntVar()
self.check_firstFlight_event = Checkbutton(self, text="First flight",
variable=self.firstFlight_event, command=self.set_response_event)
self.check_firstFlight_event.grid(row=4, column=1, padx=5)
self.Berlin_Wall_event = IntVar()
self.check_Berlin_Wall_event = Checkbutton(self, text="Berlin Wall",
variable=self.Berlin_Wall_event, command=self.set_response_event)
self.check_Berlin_Wall_event.grid(row=4, column=2, padx=5)
self.millennium_event = IntVar()
self.check_millennium_event = Checkbutton(self, text="Millennium",
variable=self.millennium_event, command=self.set_response_event)
self.check_millennium_event.grid(row=4, column=3, padx=5)
#user answers if they think they're old and if they want to know how
# old they'll be in 10, 15, or 20 years
self.prompt = Label(self, text="Do you consider yourself old?")
self.prompt.grid(row=5, column=0, pady=5)
self.feel_old = IntVar()
self.feel_old.set(4)
self.not_sure_old = Radiobutton(self, text="Not sure",
variable=self.feel_old, value="0")
self.not_sure_old.grid(row=6, column=0)
self.no_old = Radiobutton(self, text="No",
variable=self.feel_old, value="1")
self.no_old.grid(row=6, column=1)
self.yes_old = Radiobutton(self, text="Yes",
variable=self.feel_old, value="2")
self.yes_old.grid(row=6, column=2)
#submit button
self.button_submit = Button(self, text='Submit',
command=self.submit_click)
self.button_submit.grid
self.button_submit.grid(row=9, column=3, padx=10)
self.my_name = StringVar()
self.message = Label(self, textvariable=self.my_name)
self.message.grid(columnspan=2, pady=10)
self.my_age = StringVar()
self.message = Label(self, textvariable=self.my_age)
self.message.grid(columnspan=2, pady=10)
#response
def set_response_event(self):
#remembering events
if self.wheel_event.get() == 1:
self.event = "wheel"
elif self.firstFlight_event.get() == 1:
self.event = "firstFlight"
elif self.Berlin_Wall_event.get() == 1:
self.event = "Berlin_Wall"
elif self.millennium_event.get() == 1:
self.event = "millennium"
def set_response_old(self):
#feeling old
if self.not_sure_old.get() == "0":
self.old = "not_sure_old"
elif self.no_old.get() == "1":
self.old = "no_old"
elif self.yes_old.get() == "2":
self.old = "yes_old"
def submit_click(self):
output_message = 'Well ' + self.input_name.get() + ', to begin with you are ' + self.input_age.get() + '.\n'
output_message += 'You remember the ' + self.event +'.\n'
output_message += 'This means you are ' + self.old + '.'
self.my_name.set(output_message)
frame05 = MyFrame()
frame05.mainloop()
Here is what I get:
I realize I'm probably doing this the hard way but I feel like I'm really close. Thank you for your help!
First off, you are never calling set_response_old(self) that I can tell. Secondly you are trying to compare strings to integers, which doesn't get caught in your if statements. Try rewriting the last two functions like this:
def set_response_old(self):
#feeling old
if self.feel_old.get() == 0:
self.old = "not sure old"
elif self.feel_old.get() == 1:
self.old = "no old"
elif self.feel_old.get() == 2:
self.old = "yes old"
def submit_click(self):
self.set_response_old()
output_message = 'Well ' + self.input_name.get() + ', to begin with you are ' + self.input_age.get() + '.\n'
output_message += 'You remember the ' + self.event + '.\n'
output_message += 'This means you are ' + self.old + '.'
self.my_name.set(output_message)
When I run this it outputs
This means you are yes old.
Which I think is what you were going for.
#EDIT
As for getting the list of remembered events to show...You can add self.events_remembered = [] to your __init__ section to create a place to store the data (it's not required to be in the init section as the code below would make it anyways, but it's good practice!) and then change your set_response function as such:
def set_response_event(self):
#remembering events
self.events_remembered = [] # reset each time we are determining pressed events (so we don't just keep adding duplicates, and also remove any unchecked boxes)
if self.wheel_event.get() == 1:
self.events_remembered.append("wheel")
if self.firstFlight_event.get() == 1:
self.events_remembered.append("firstFlight")
if self.Berlin_Wall_event.get() == 1:
self.events_remembered.append("Berlin Wall")
if self.millennium_event.get() == 1:
self.events_remembered.append("millennium")
You are correct in thinking that the if will only catch the first one, but there is no rule against having a series of if statements like shown!
Finally you can update your submit_click function to search for this information and display the formatted list
def submit_click(self):
self.set_response_old()
self.set_response_event() # use the function we just updated
print(self.events_remembered)
output_message = 'Well ' + self.input_name.get() + ', to begin with you are ' + self.input_age.get() + '.\n'
output_message += 'You remember the ' + ", ".join(self.events_remembered) + '.\n'
output_message += 'This means you are ' + self.old + '.'
self.my_name.set(output_message)
Here we use ", ".join(iterable_list) to convert our python list to a nice comma separated list instead!
If this solved your question I would appreciate if you could accept the answer by clicking the check mark next to the post! If you really just like me you can click to upvote me too XD
To get all the checkbox values to the output, make self.event attribute a list
def __init__self():
...
...
self.event=[]
and when you call the set_response_event function, append those string values to that list.
def set_response_event(self):
#remembering events
if self.wheel_event.get() == 1:
self.event.append("wheel")
elif self.firstFlight_event.get() == 1:
self.event.append("firstFlight")
elif self.Berlin_Wall_event.get() == 1:
self.event.append("Berlin_Wall")
elif self.millennium_event.get() == 1:
self.event.append("millennium")
Finally print the list using .join function
'You remember the ' + ', '.join(self.event) + '\n'

Changing text of labels defined in a loop

I'm trying to make a scalable counter.
In the first window you enter how many counters you want.
In the second window there are labels and buttons used to add one to the label.
Here is my code:
from tkinter import *
root = Tk()
def newWindow():
window = Toplevel()
for i in range(int(textbox.get())):
exec("global label"+ str(i))
exec("label" + str(i) + " = Label(window, text = '0')")
exec("label" + str(i) + ".grid(row = 0, column = i)")
exec("global button"+ str(i))
exec("button" + str(i) + " = Button(window, text = 'Add', command = lambda: setText(label" + str(i) + "))")
exec("button" + str(i) + ".grid(row = 1, column = i)")
def setText(label):
label.config(text = str(int(label.cget("text")) + 1))
textbox = Entry(root)
textbox.grid(row = 0)
submitButton = Button(root, text = "Submit", command = newWindow)
submitButton.grid(row = 0, column = 1)
root.mainloop()
However this is the error I get:
name 'label_' is not defined
where _ is i.
Making them global didn't fix this either.
Help please!
If you're using exec in this way, you're doing something very wrong.
The simple solution is to add widgets to a list or dictionary. Though, in this specific case you don't need that because you're never referencing the label anywhere but in the button command.
Here's a working example:
from tkinter import *
root = Tk()
def newWindow():
global labels
window = Toplevel()
labels = {}
for i in range(int(textbox.get())):
label = Label(window, text='0')
button = Button(window, text='Add', command = lambda l=label: setText(l))
label.grid(row=0, column=i)
button.grid(row=1, column=i)
# this allows you to access any label later with something
# like labels[3].configure(...)
labels[i] = label
def setText(label):
label.config(text = str(int(label.cget("text")) + 1))
textbox = Entry(root)
textbox.grid(row = 0)
submitButton = Button(root, text = "Submit", command = newWindow)
submitButton.grid(row = 0, column = 1)
root.mainloop()
If you wanted to make use of labels, you could have your button pass in the index, and let setText get the widget from the dictionary:
def setText(i):
label = labels[i]
label.configure(...)
...
button = Button(..., command=lambda i=i: setText(i))

Python Tkinter- Direct pointer back to Entry() box

When A user inputs a blank string of text I can either pop up a new input box which looks nasty or, like a webpage, direct the cursor back into the Entry() box
Unfortunately after searching I am still completely clueless as to how I can achieve this direction of the cursor.
My code looks like this-
import time
from tkinter import *
root = Tk()
##Encrypt and Decrypt
Master_Key = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#£$%&'()*+,-./:;?#[\\]^_`{|}~\t\n\r\x0b\x0c"
def Encrypt(User_Input, Key):
Output = ""
for i in range(len(User_Input)):
Ref_For_Output = Master_Key.index(User_Input[i]) + Master_Key.index(Key[i])
if Ref_For_Output >= len(Master_Key):
Ref_For_Output -= len(Master_Key)
Output += Master_Key[Ref_For_Output]
return Output
def Decrypt(User_Input, Key):
Output = ""
for i in range(len(User_Input)):
Ref_For_Output = Master_Key.index(User_Input[i]) - Master_Key.index(Key[i])
if Ref_For_Output < 0:
Ref_For_Output += len(Master_Key)
Output += Master_Key[Ref_For_Output]
return Output
##def popup():
## main = Tk()
## Label1 = Label(main, text="Enter a new key: ")
## Label1.grid(row=0, column=0)
## New_Key_Box = Entry(main, bg="grey")
## New_Key_Box.grid(row=1, column=0)
##
## Ok = Button(main, text="OK", command=Set_Key(New_Key_Box.get()))
##
## Ok.grid(row=2, column=0)
## if
## main.geometry("100x300")
## main.mainloop()
## return New_Key_Box.get()
class MyDialog:
def __init__(self, parent):
top = self.top = Toplevel(parent)
Label(top, text="Value").pack()
self.e = Entry(top)
self.e.pack(padx=5)
b = Button(top, text="OK", command=self.ok)
b.pack(pady=5)
def ok(self):
print( "value is" + self.e.get())
return self.e.get()
self.top.destroy()
def Compatibility(User_Input, Key):
while Key == "":
root = Tk()
Button(root, text="Hello!").pack()
root.update()
d = MyDialog(root)
print(d.ok(Key))
root.wait_window(d.top)
Temp = 0
while len(Key) < len(User_Input):
Key += (Key[Temp])
Temp += 1
return Key
##Layout
root.title("A451 CAM2")
root.geometry("270x80")
Label1 = Label(root, text="Input: ")
Label1.grid(row=0, column=0, padx=10)
Label2 = Label(root, text="Key: ")
Label2.grid(row=1, column=0, padx=10)
Input_Box = Entry(root, bg="grey")
Input_Box.grid(row=0, column=1)
Key_Box = Entry(root, bg="grey")
Key_Box.grid(row=1, column=1)
def Encrypt_Button_Press():
User_Input = Input_Box.get()
Key = Compatibility(User_Input, Key_Box.get())
print(User_Input)
root.clipboard_append(Encrypt(User_Input, Key))
Encrypt_Button.configure(text="Encrypting")
messagebox.showinfo("Complete", "Your encrypted text is: \n" + Encrypt(User_Input, Key) + "\n The text has been added to your clipboard.")
Encrypt_Button.configure(text="Encrypt")
#popup()
def Decrypt_Button_Press():
User_Input = Input_Box.get()
Key = Key = Compatibility(User_Input, Key_Box.get())
print(User_Input)
root.clipboard_append(Decrypt(User_Input, Key))
Decrypt_Button.configure(text="Decrypting")
messagebox.showinfo("Complete", "Your Decrypted text is: \n" + Decrypt(User_Input, Key) + "\n The text has been added to your clipboard.")
Decrypt_Button.configure(text="Decrypt")
Encrypt_Button = Button(text="Encrypt", command=Encrypt_Button_Press)
Encrypt_Button.grid(row=0, column=3, padx=10)
Decrypt_Button = Button(text="Decrypt", command=Decrypt_Button_Press)
Decrypt_Button.grid(row=1, column = 3, padx=10)
root.mainloop()
In the compatibility function I am wanting to change the while Key == "":
to pop-up a message (that's easy) and to direct the cursor back to the Key_Box( I may also make it change to red or something)
So- does anyone know how I can achieve redirection of the cursor?
Edit:I am not sure whether this is included anywhere in Tkinter, I can use tab to switch between Entry() boxes so I assume that they function in roughly the same way as other entry boxes across different platforms.
You could call .focus() on the entry? It won't move the cursor, but the user would be able to just start typing away in the entry box as if they had clicked in it.

Categories