Tkinter While loop int issue

Question:

import time
from tkinter import* 

start_value=Entry(root,width=10)
start_value.pack()
start_value.get()

while int(start_value.get())<100:
   int(start_value.get()) += 1

Because of the int(start_value.get()) += 1 on this code my loop doesn’t work.

Asked By: YagizWasTaken

||

Answers:

the issue arises from the fact that you are creating an Entry, which you are assigning as an empty string. So, when you try to convert start_value into an int, you are trying to convert an empty string into an int, hence the error. You can fix this by setting the initial value of the entry to 0 (or whatever int you want), incrementing that value, deleting the initial value, and then reinserting the new value. I have provided an example of the entry value incrementing on its own until it reaches 100.

import time
from tkinter import* 

root = Tk()

start_value=Entry(root,width=10)
start_value.pack()
start_value.insert(0, 0)

def increase_value():
    value = int(start_value.get())
    value += 1
    start_value.delete(0, 'end')
    start_value.insert(0, value)

    if value < 100:
        # increase the value every 100 milliseconds
        root.after(100, increase_value)

increase_value()

root.mainloop()
Answered By: Amari
import string
from tracemalloc import start
from pyautogui import *
import pyautogui
import time
import keyboard
import numpy as np
import random
import win32api, win32con
import math
import tkinter as tk
from tkinter import*    

root=tk.Tk()
root.geometry("500x550+50+100")
root.title("Led Controler by Yağız Arslan")

print("system is online")





def buttonclick12():
    start_value_int = start_value.get()
    end_value_int=end_value.get()
    print(start_value_int)
    print(end_value_int)
    loop_number=int(end_value_int) + 1
    loop_total= int(start_value_int)
    
    while int(loop_number) > int(loop_total) :
        print("NFT_" +str(loop_total) + " generated")  
        loop_total += 1 




start_value=Entry(root,width=10)
start_value.pack()

end_value=Entry(root,width=10)
end_value.pack()

#loop_number =  
collection_name=Entry(root,width=10)
collection_name.pack()
price=Entry(root,width=10)
button1=Button(root,text="Start",command=buttonclick12)
button1.pack()
loop_total = 0


        


root.mainloop()```
Answered By: YagizWasTaken
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.