I need a window with an input box and an output box. In the input box I set a number (int) the program has to multiply it by 2 every one(1) second and write the result on the outbox.
I started with this:
import time
import tkinter as tk
from tkinter import *
master = tk.Tk()
#####################################
int1 = 2 #this is the number I need to write in the input box
#######################################
abc = tk.IntVar(master, value= int1)
#print(abc.get())
out = abc.get()
mylabel= tk.Label(master, text= out)
mylabel.pack()
#####################################
master.mainloop()
while int1 <= 100:
time.sleep(1)
int1 += 2
print(int1)
# I tryed to integrate this block above but without success.
The problem is that master.mainloop()
blocks until the window is closed. So when your while
loop starts the user cannot see the output anymore. The solution is to use master.after
to schedule a function to run after one second.
You also had mixed problems with the IntVar
that would cause it not to be updated, so I've rewritten how IntVar and Label are created.
import time
import tkinter as tk
from tkinter import *
master = tk.Tk()
#####################################
start_value = 2 #this is the number I need to write in the input box
#######################################
counter = tk.IntVar(master, value=start_value)
# Use `textvariable=` instead of `text=`, or the counter won't update!
mylabel= tk.Label(master, textvariable=counter)
mylabel.pack()
def increment_number():
# Get the value from IntVar and updated it.
# The label will update automatically because we created it
# with `textvariable=counter`.
counter.set(counter.get() * 2)
# Continue increment the number after one second.
master.after(1000, increment_number)
# Start incrementing the number after one second.
master.after(1000, increment_number)
#####################################
master.mainloop()
As for the input box, I'd suggest you give it a try yourself first. Feel free to create another question if you run into problems with that part.