Python/Tkinter - How can I create infinite copies of a window? - python

I want to make a window that is constantly created making many copies of its-self.
I have tried making an infinite loop that calls the function every time but that doesn't work. I also tried making one within the function but it just recreates the window after I close the old one. Does this mean that the window can't be duplicated?
import tkinter
import os
def win():
A = True
while A == True:
window = tkinter.Tk()
window.title('WHOOPS')
window.geometry('100x100')
window.configure(background= "green")
lbl = tkinter.Label(window, text= 'whoops', bg= 'red')
window.mainloop()
quit = False
while quit == False:
win()
Can someone show me how to duplicate a window without hard-coding a whole new block, for example, a new function for each of win2() and win3()?
The user should be able to see a practically infinite stream of windows that can only be stopped by using the KeyboardInterrupt or by closing the shell/interpreter.

If you create windows in a infinite loop you are never going to return control to the main_loop. You can follow one of the followings:
#1 You can initialize an integer to 0 and after a specific count update root and then again continue creating windows and after a specific count update root again and it goes on ...
import tkinter as tk
import time
def startInfiniteLoop():
i = 0
otherFrame = []
while True:
otherFrame.append(tk.Toplevel())
i += 1
if i%1000 == 0:
time.sleep(1)
root.update()
root = tk.Tk()
button_one = tk.Button(root, text="Infinite Window !", command=startInfiniteLoop)
button_one.grid(row=0, column=0)
root.mainloop()
#2 You can write a function that creates a window and from inside of that function call itself after specific time again and again.
import tkinter as tk
def startInfiniteLoop():
otherFrame = tk.Toplevel()
root.after(1000, startInfiniteLoop)
root = tk.Tk()
button_one = tk.Button(root, text="Infinite Window !", command=startInfiniteLoop)
button_one.grid(row=0, column=0)
root.mainloop()

Related

How to stop the button's command by clicking another button

Let's say I have button1 which repeatedly changes its position randomly when clicked. I want to have button2 which will stop button1's action, but I haven't found any helpful method for that
from tkinter import *
def move():
# my code
def stop():
#???
master = Tk()
Button1 = Button(master, #some options, command = move).pack()
Button2 = Button(master,#some options, command = stop).pack()
master.mainloop()
So this code I am going to introduce after my research will hopefully satisfy what you need. So first of all, you need a function to move buttons around and secondly, another function to stop the working of the first one's function (i.e., to prevent its movement).
So one way of doing this is to set the state of the other Buttons to DISABLED to grey them out and prevent clicks. So the one I have shown below uses this method.
# Importing tkinter module
from tkinter import *
# Importing random module
import random
# Creating a tkinter window
root = Tk()
# Initialize tkinter window with dimensions 300 x 250
root.geometry('300x250')
# Stop other function
def stop():
btn.config(state=DISABLED)
# Defining func to move on click
def move():
x = random.randint(50,250)
y = random.randint(50,200)
btn.place(x=x, y=y)
# Creating a moving Button
btn = Button(root, text = 'Click me!',command=move)
btn.pack()
# Creating an end movement Button
endbtn = Button(root, text = 'Stop movement!', command=stop).pack()
root.mainloop()
Now coming to the second method, you can use another function that does nothing to modify the movement button's function. An example of this is given below:
# Importing tkinter module
from tkinter import *
# Importing random module
import random
# Creating a tkinter window
root = Tk()
# Initialize tkinter window with dimensions 300 x 250
root.geometry('300x250')
# Stop other function
def stop():
btn.config(command=donothing)
# A function to do nothing
def donothing():
pass
# Defining method on click
def move():
x = random.randint(50,250)
y = random.randint(50,200)
btn.place(x=x, y=y)
# Creating a moving Button
btn = Button(root, text = 'Click me!',command=move)
btn.pack()
# Creating an end movement Button
endbtn = Button(root, text = 'Stop movement!', command=stop).pack()
root.mainloop()
And the last method is to destroy the whole button. You can use the second button to destroy the first button. An example is given for your reference.
# Importing tkinter module
from tkinter import *
# Importing random module
import random
# Creating a tkinter window
root = Tk()
# Initialize tkinter window with dimensions 300 x 250
root.geometry('300x250')
# Destroy the other button
def stop():
btn.destroy()
# Defining method on click
def move():
x = random.randint(50,250)
y = random.randint(50,200)
btn.place(x=x, y=y)
# Creating a moving Button
btn = Button(root, text = 'Click me!',command=move)
btn.pack()
# Creating an end movement Button
endbtn = Button(root, text = 'Stop movement!', command=stop).pack()
root.mainloop()

How to display a value on Tkinter window from a function every second?

So I'm working on a function does something (turns a motor) every second. For counting the second I use time.sleep(1) because I don't know any other way to wait.
def wash_loop(wash_time):
count = 0
dist = 0
global error_flag
error_flag = 0
while (count < wash_time):
if(count%2==0):#going forward
GPIO.output(Motor_IN1,GPIO.HIGH)
GPIO.output(Motor_IN2,GPIO.LOW)
GPIO.output(Motor_EN,GPIO.HIGH)
print(count)
time.sleep(MOTOR_SLEEP)
else:#going backwards
GPIO.output(Motor_IN1,GPIO.LOW)
GPIO.output(Motor_IN2,GPIO.HIGH)
GPIO.output(Motor_EN,GPIO.HIGH)
print(wash_time - count) #want to display this on a Tkinter window instead
time.sleep(MOTOR_SLEEP)
count+=1
dist = distance_check()
if(dist < CRITICAL_DIS):
error_alert()
break
if(count>=wash_time):
GPIO.output(Motor_EN, GPIO.LOW)
break
The Tkinter function that I'm trying to do this in looks like this:
def wash_window():
#create the main window for GUI control applcation
window2 = TK.Tk()
temp = TK.IntVar()
window2.geometry(WINDOW_GEOMETRY)
window2.title("Wash Cycle ")
#create the frame that will hold instructions
frame0 = TK.Frame(window2)
frame0.pack(side=TK.TOP)
TK.Label(frame0, text="You've selected custom wash cycle").grid(row=0,column=0)
TK.Label(frame0, text="Please select the water temperature for the wash.")\
.grid(row=1,column=0)
frame1 = TK.Frame(window2)
frame1.pack(side=TK.TOP)
temp.get()
hot_button = TK.Radiobutton(frame1, text="HOT", variable=temp, value=1 ).grid(row=0, column=0)
cold_button = TK.Radiobutton(frame1, text="COLD", variable=temp, value=2).grid(row=0,column=1)
warm_button = TK.Radiobutton(frame1, text="WARM", variable=temp, value=3).grid(row=0,column=2)
#create the frame that will hold control entry in the window
frame2 = TK.Frame(window2)
frame2.pack(side=TK.TOP)
TK.Label(frame2, text = "Please enter the time below for the wash cycle").grid(row=0,column=0)
user_entry = TK.Entry(frame2)
user_entry.grid(row=1,column=0)
frame3= TK.Frame(window2)
frame3.pack(side=TK.TOP)
start_button = TK.Button(frame3, text="START", command = lambda: wash_cycle(user_entry,temp)).grid(row=0, column=0)
# stop_button = TK.Button(frame3, text="STOP", command = lambda: wash_cycle(user_entry,temp)).grid(row=0, column=1)
quit_button = TK.Button(frame3, text="QUIT", command = window2.destroy).grid(row=0, column=2)
What I'm trying to do is display the countdown (from the entered time to 0) on this Tkinter window as soon as the person presses start in the window. This is different from using the after() function because I want to show a value from a function which is only executing once, only with the exception that it has a loop.
Thank You!
So what you could do is work with threading.
You Need to:
from threading Import Timer
Then you create a timer
your_timer = Timer(the_seconds_you_want_to_wait, the_function_to_call)
You are calling a function with this and in that function you can display anything in your tkinter window.
What I recommend you to do here is create a Label and then change the properties of it.
For example if you want to show a number on a label:
L1 = Label(root, text="your text here")
L1.pack()
Now if you want to edit that later:
L1.configure(text="your new text here")
your_timer.start()
And at the end of your function you Need to Start the timer in order to the create a cycle or just create a Loop for it.

How to add time (ticks) in a GUI for a game

I want to add the ticking of time in seconds to a GUI, for a clicker game. So the idea is to have a function that gets called every n ticks, and this function increments X objects.
I have tried to use a while loop, both before and after calling the .mainloop() method. It didn't work in either occasion, I also tried the crazy idea of having the mainloop() method inside the while loop (aware of what that would do lol).
from tkinter import *
import time
result = 0
window = Tk()
window.title("Numbers Game")
window.geometry('360x240')
label = Label(window, text=result)
label.grid(column=0,row=0)
def clicked():
global result
result += 1
label.config(text=result)
button = Button(window, text="Push Me", command=clicked)
button.grid(column=1, row=2)
window.mainloop()
while True:
time.sleep(1)
clicked()
The current version of my code produces an error that mentions the function doing GUI related things outside of the window. But I don't have the slightest clue of how to achieve this.
You mean you want to have the result counter increment every second? You can't use infinite loops with a GUI, because they interfere with the GUI's mainloop. You have to integrate your code into the mainloop using the after method.
from tkinter import *
import time
result = 0
window = Tk()
window.title("Numbers Game")
window.geometry('360x240')
label = Label(window, text=result)
label.grid(column=0,row=0)
def clicked():
global result
result += 1
label.config(text=result)
def tick():
clicked()
window.after(1000, tick) # after 1,000 milliseconds, call tick() again
button = Button(window, text="Push Me", command=clicked)
button.grid(column=1, row=2)
tick() # start the "loop"
window.mainloop()

Tkinter code isnt executed after destroying a window

from tkinter import *
from random import *
root = Tk()
#A function to create the turn for the current player. The current player isnt in this code as it is not important
def turn():
window = Toplevel()
dice = Button(window, text="Roll the dice!", bg= "white", command=lambda:diceAction(window))
dice.pack()
window.mainloop()
#a function to simulate a dice. It kills the function turn.
def diceAction(window):
result = Toplevel()
y = randint(1, 6)
# i do something with this number
quitButton = Button(result, text="Ok!", bg="white", command=lambda: [result.destroy(), window.destroy()])
quitButton.pack()
window.destroy()
result.mainloop()
#A function to create the playing field and to start the game
def main():
label1 = Button(root, text="hi", bg="black")
label1.pack()
while 1:
turn()
print("Hi")
turn()
main()
root.mainloop()
With this code i basically create a roll the dice simulator. In my actual code i give the function turn() player1/player2(which are class objects) so i can track whose turn it is. Thats why i call turn() 2 times in the while.
The problem is that the code after the first turn() isnt executed(until i manually close the root window which is weird) anymore. At my knowledge this should work.
I open the turn function which opens the diceAction function upon button press. diceAction() gives me the number and kills both windows. Then the second turn() should be called and the process continues until someone wins(which i havent implemented in this code).
The print("Hi") isnt executed either. Am i missing something? You can copy this code and execute it yourself.
The short answer is "Infinite loops and tkinter don't play well together". The long answer is that you never escape window.mainloop(). I don't see a good enough reason that you need to have window.mainloop() and result.mainloop() running to justify the headache of multiple loops in tkinter.
A subjectively better way of doing this is to have the end of the first turn() trigger the start of the next one:
from tkinter import *
from random import *
root = Tk()
global turnCount
turnCount = 0
def turn():
window = Toplevel()
dice = Button(window, text="Roll the dice!", bg="white", command=lambda:diceAction())
dice.pack()
def diceAction():
result = Toplevel()
y = randint(1, 6)
quitButton = Button(result, text="Ok!", bg="white", command=lambda: nextTurn())
quitButton.pack()
def nextTurn():
global turnCount
turnCount = turnCount + 1
for i in root.winfo_children():
if str(type(i)) == "<class 'tkinter.Toplevel'>":
i.destroy()
turn()
def main():
label1 = Button(root, text="hi", bg="black")
label1.pack()
turn()
main()
root.mainloop()
I would recommend attempting to use OOP on a project like this instead of the global that I declared above.

How to get the entry text and display in another entry?

I just want that when I type my name inside the entry box then appears in another entry with some add text. The idea is type in the entry below and after that it showed in the big entry.I was looking for this solution, but just found place in Label. I don't want in Label. The window is more big, must drag to show the entry. There's is a picture that i use in this script:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
cat = Entry(root)
cat.place(x=48, y=25, width= 350, height=140)
user = Entry(root)
user.place(x=75, y=550)
btn = Button(root, text='START')
btn.place(x=220, y=410)
root.mainloop()
#
Ok, It works the way you told me,thank you!
But now i'm facing another problem.
The problem is when i insert the function of the game in the second window. I tested in one window and it works, but when i place the function in the second window gives an error when i press the "Start" button:
'''user_try = int(txt.get())
NameError: name 'txt' is not defined'''
When i press reset button gives another error:
'''user_try = int(txt.get())
NameError: name 'txt' is not defined'''
So i know that is missing definition, but i don't know how to make a reference for this command that it's in the second window. Like i said running with just one window the program works.
Maybe i should make using class, i don't know, but i wish to make this way that i started. However if there's no other way to do as i'm doing, let's go.
I just simplify the script here, actualy the main script is more bigger, so my idea is when open the program, there is a window and the user read the instructions about the game and proceed open the second window. The window have pictures and some hidden buttons in the next picture, so there will be an interactivity with the environment.
The guess number is just the beggining. After that there will be new challeges.
I'm very excited doing this, but i'm stuck in this point. The part one i finished, the pictures, the hidden buttons it's exacly the way i want, but the challenge stops here in this problem.
from tkinter import *
from PIL import Image, ImageTk, ImageSequence
import random
from tkinter import messagebox
pc = random.randint(1,10)
def reset():
global pc
pc = random.randint(1,10)
cat['text'] = 'Ok! Lets Try Again!'
def openwin2():
win1.withdraw()
win2 = Toplevel()
win2.geometry('350x300+180+100')
win2.title('second window')
txt = Entry(win2)
txt.place(x=10,y=10)
cat = Label(win2,wraplength=300)
cat.place(x=10,y=50)
cat.config(text='Hi! I Am thinking a number between 1 and 10.')
btn = Button(win2,text='start',command=check)
btn.place(x=30, y=150)
btn2 = Button(win2, text='reset', command=reset)
btn2.place(x=110,y=150)
win2.mainloop()
def check():
user_try = int(txt.get())
if user_try < pc:
msg = 'Hmmmm... the number, which I thought of, is greater than this.'
elif user_try > pc:
msg = 'How about trying a smaller number ?!'
elif user_try == pc:
msg = 'Well Done! You guessed! It was %s the number!' % user_try
else:
msg = 'Something Went Wrong...'
cat['text'] = msg
win1 = Tk()
win1.title('First Window')
win1.geometry('350x300')
user = Label(win1,text='first window')
user.place(x=10,y=10)
btn1 = Button(win1,text='Open Window 2', command=openwin2)
btn1.place(x=10,y=50)
win1.mainloop()
There are multiple ways to do this in tkinter, here's a rework of your code using StringVar objects set to the textvariable properties of your Entry objects:
import tkinter as tk
def doit():
out_string.set("Hello " + in_string.get())
root = tk.Tk()
in_string = tk.StringVar()
out_string = tk.StringVar()
cat = tk.Entry(root, textvariable=in_string)
cat.place(x=20, y=25, width=100)
user = tk.Entry(root, textvariable=out_string)
user.place(x=20, y=75)
btn = tk.Button(root, text='START', command=doit)
btn.place(x=20, y=150)
root.mainloop()
Per #Mike-SMT, here's a different approach using Entry.get() and Entry.insert(). It augments the text when the user clicks the button:
import tkinter as tk
def doit():
user.insert(tk.END, cat.get())
root = tk.Tk()
cat = tk.Entry(root)
cat.place(x=20, y=25, width=100)
user = tk.Entry(root)
user.place(x=20, y=75)
user.insert(0, "Hello ")
btn = tk.Button(root, text='START', command=doit)
btn.place(x=20, y=150)
root.mainloop()
However, you'll see that subsequent button clicks keep appending the text. When working with Entry.insert(), you need to work with Entry.delete() and/or other Entry methods to properly manipulate the text.

Categories

Resources