I used this program for few weeks and it worked perfectly. Now, without changing anything, it doesn't work. More precisely, the orientation value doesn't change when you click on the radiobutton. Then, the program PosChoisie return 0 instead of either 1 or 2.
I'm on this program for 2 days, but even if I tried hard, I didn't found the error. Thanks for your futur help.
value=StringVar()
orientation = IntVar()
radiobutton_Horizontal = Radiobutton(fen, text="Horizontal", variable=orientation, value=1).pack()
radiobutton_Vertical = Radiobutton(fen, text="Vertical", variable=orientation, value=2).pack()
def PosChoisies():
L=[]
ori=IntVar()
ori = orientation.get()
if ori == 1:
o='horizontal'
L.append(o)
#print('horizontal')
elif ori == 2:
o='vertical'
L.append(o)
#print('vertical')
print(L)
print(ori)
return L
butValider = Button(fen, text="valider", relief = RAISED, command=lambda:(print(abs,ord,o)).pack()
fen.mainloop() ```
Related
I don't understand why the top variable in the append function is greyed out.
No matter what I do, the value of top stays None.
This is the output. I also tried using 0 instead of None and I still have the same problem. I've included the code below.
top = None
stack = []
def append(a, top):
stack.append(a)
if top == None:
top = 1
else:
top += 1
def pop(top):
if top == None:
print('underflow error encountered')
else:
print('the popped element is:', poppedele)
print(top)
while True:
kk = input('wat do you want to do ....')
if kk == '1':
a = input('enter the element to append')
append(a, top)
print(stack, top)
elif:
pop(top)
print(stack, top)
else:
break
Here's a screenshot of what it looks like:
Thanks #aarni joensuu.
Turns out I should add global top in those functions only then the change will be made to the overall variable.
When I run this code:
currentquestion = 0
currentcheckbox = 1
which_radio_var = StringVar(inside_test_root)
while currentquestion < len(questions):
print('currentcheckbox', currentcheckbox)
Radiobutton(inside_test_root, text=questions[currentquestion], value=currentcheckbox, variable=which_radio_var, indicatoron=0, wraplength=30).grid(row=currentquestion+1, column=0)
currentquestion += 2
currentcheckbox += 1
x = which_radio_var.get()
I get the Radiobuttons coming up fine and everything works properly, apart from wen I try to test these radiobuttons with the code:
Button(inside_test_root, text='oof', command=print(x)).grid(column = 77, row = 77)
Nothing prints. Is it something to do with the nature i made the radiobuttons? I need a way to not know the amount of buttons that are being produced beforehand.
Any help would be much appreciated, thanks
Answer is as jasonharper said, I missed out the lambda, meaning that i was calling the print function the first time and never again
"command=print(x) means to print x right now, and use the return value of the print() function (which is None) as the command to execute when the button is clicked. command=lambda: print(which_radio_var.get()) would do the job. "
Updated code is now: (the variable 'x' has become 'radio' to fit with the rest of my program)
if questiontype == 'Multiple Choice':
currentquestion = 0
currentcheckbox = 1
which_radio_var = StringVar(inside_test_root)
print (which_radio_var)
while currentquestion < len(questions):
print('currentcheckbox', currentcheckbox)
Radiobutton(inside_test_root, text=questions[currentquestion], value=currentcheckbox, variable=which_radio_var, indicatoron=0, wraplength=30).grid(row=currentquestion+1, column=0)
currentquestion += 2
currentcheckbox += 1
print(which_radio_var.get())
radio = which_radio_var.get()
Button(inside_test_root, text='oof', command=lambda: print(radio)).grid(column = 77, row = 77)
I know my question sounds very much like many previous questions but I can honestly not figure it out in the context of my program. I have an algorithm for the Collatz Conjecture that I would like to run through a Tkinter GUI (everything works just fine through the terminal).I have tried to bind the relevant function to the Return key and to a button but I get the same error message for both methods of entering data, which I will show below. I get the output to work perfectly on the GUI if I input through the terminal.
What I have tried is best explained through the code below. (The code above the #### line has mostly to do with making the GUI appear over my Spyder IDE and not hiding behind it.)
Code:
from tkinter import *
root = Tk()
import os
import subprocess
import platform
def raise_app(root: Tk):
root.attributes("-topmost", True)
if platform.system() == 'Darwin':
tmpl = 'tell application "System Events" to set frontmost of every process whose unix id is {} to true'
script = tmpl.format(os.getpid())
output = subprocess.check_call(['/usr/bin/osascript', '-e', script])
root.after(0, lambda: root.attributes("-topmost", False))
########################################################################
lst = []
def collatz(num):
while num != 1:
lst.append(num)
if num % 2 == 0:
num = int(num / 2)
else:
num = int(3 * num + 1)
def main(event):
collatz(num)
#Input Box
input = Entry(root, width = 10, bg = "light grey")
input.grid(row = 0, column = 0, sticky = W)
input.get()
input.bind("<Return>", main)
##Button
#button1 = Button(root, width = 10, text = "Run", command = main)
#button1.grid(row = 1, column = 0, sticky = W)
##Output box
output1 = Text(root, width = 100, height = 10, bg = "light grey")
output1.grid(row = 3, column = 0, sticky = W)
output2 = Text(root, width = 50, height = 1, bg = "white")
output2.grid(row = 2, column = 0, sticky = W)
output1.insert(END, lst)
output2.insert(END, "Number of iterations are: " + str(len(lst)))
########################################################################
raise_app(root)
root.mainloop()
When I run the code as is, the input box appears but when I click return, I get an error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Users/andrehuman/Desktop/Python/programs/Collatz Conjecture/Collatz_alt3.py", line 43, in main
collatz(num)
NameError: name 'num' is not defined
Exactly the same if I try and link a button to the "main" function.
When I comment the input and buttons out, and enter the number through the terminal, everything works as expected. The list of iteration numbers appear in the text box as it should. (And I can even get a Matplotlib graph to display the data visually in the terminal.) If I can get this problem sorted out I want to try and display (or embed) the Matplotlib graph in the GUI.
Anyway, that's it. Any help will be greatly appreciated.
Andre Human
name 'num' is not defined occurs because you're calling collatz(num), but the program does not understand what value you are referring to when you say num. You should assign a value to that name before using it. I assume you want the value to be the contents of your input box.
def main(event):
num = int(input.get())
collatz(num)
You will also need to copy your output1.insert and output2.insert lines to the inside of main. Right now, those lines execute before the window even appears to the user, so there's no way that they can enter a number fast enough to get collatz to trigger before the text gets written. And changing lst after the fact does nothing to the text, since it's not smart enough to notice that the list has changed.
def main(event):
num = int(input.get())
collatz(num)
#delete previous contents of text boxes
output1.delete(1.0, END)
output2.delete(1.0, END)
output1.insert(END, lst)
output2.insert(END, "Number of iterations are: " + str(len(lst)))
Another problem is that successive calls to collatz will cause lst to grow and grow, because the contents of the list from previous calls is still present. Try entering 4 into the text box, and press Enter a few times. The output will go from 2 to 4 to 6... That's not right.
This is something of a natural hazard when using mutable global state. One possible solution is to reset lst at the beginning of each collatz call.
def collatz(num):
lst.clear()
#rest of function goes here
... But I'm more inclined to make lst local to the function, and return it at the end.
def collatz(num):
lst = []
while num != 1:
lst.append(num)
if num % 2 == 0:
num = int(num / 2)
else:
num = int(3 * num + 1)
return lst
def main(event):
num = int(input.get())
lst = collatz(num)
#delete previous contents of text boxes
output1.delete(1.0, END)
output2.delete(1.0, END)
output1.insert(END, lst)
output2.insert(END, "Number of iterations are: " + str(len(lst)))
#later, just before mainloop is called...
#lst doesn't exist in this scope, so just set the text to a literal value
output1.insert(END, "[]")
output2.insert(END, "Number of iterations are: 0")
So I have a currency which is increasing (that system is working fine). The first part updates the label every 100 ms. I have another button which triggers the second function which is supposed to clear the labels from the first. It sets home_status equal to 0 which should in theory run Money.place_forget() to clear the code. I have tested each part individually and it works but when I put the clears inside the elif statement it doesn't. It does not give me any errors, it just simply doesn't do anything (it does print END OF UPDATE HOME so the elif is triggered).
Any suggestions?
def updatehome(self):
print("UPDATE HOME")
global buy_button, home_status, currency
MoneyLabel = Label(self, text = "Money: ")
MoneyLabel.place(x = 5, y = 70)
Money = Label(self, text=currency)
Money.place(x = 50, y = 70)
if (home_status == 1):
self.after(100, self.updatehome)
elif (home_status == 0):
print("END OF UPDATE HOME")
Money.place_forget()
MoneyLabel.place_forget()
def clearhome(self):
print("CLEAR HOME")
global home_status
home_status = 0
you are creating ten labels every second, all stacked on top of each other, but you are only deleting the very last label you create.
Im in a need of a little help here, i feel like i have searched endlessly and been unable to corret my problem.
The case:
I have a checkbutton which have on value as 1 and off as 0, now i want to do an act if it is on and another if it is off.
My code goes:
#==Checkbox==#
check = IntVar()
checkbox = Checkbutton(labelframe, text="Tillad mere end én linje", variable = check, onvalue=1, offvalue=0)
checkbox.pack(side = RIGHT)
...
def go():
check.get()
print(check)
if(check == 0):
print("off")
w.delete(ALL)
tegnefladen()
update()
else:
print("on")
update()
You aren't actually setting the value. check is an object, and it won't ever be identical to 0. Basically, you want to compare check.get(). Try this:
def go():
print(check.get())
if(check.get() == 0):
print("off")
w.delete(ALL)
tegnefladen()
update()
else:
print("on")
update()