hi, is there any way to restart Tkinter program while running.
I would like to put some button that will when pressed put everything as it was in moment I started program
thx
We have here code snippet by Gribouillis
http://www.daniweb.com/software-development/python/code/260268
to restart running program, but usually you should be able to just exit from mainloop and restart by putting the call to mainloop in while loop.
and how do I putt call to main loop?
I do not understand, you can not have tkinter program without starting mainloop.
You would have to store all of the initial values and change the values back when the button in pressed. Or, generally you use a class to run a Tkinter application so you can destroy the class instance and create it again. Some example code would help us help you as we don't know what "everything" in "put everything as it was in moment I started program" means.
here is a little cut from code.
I would like to have button that will put everything like I just started program
import tkinter as tk
import random
from functools import partial
from math import sqrt
popis = []
gotove=[]
igrace=[]
kartica = []
pr=[False]
kontrola=[True]
igrac=[False]
okrenute= [0,0]
p=[0,0]
b = 0
v=int(4)
popis = 2 * [x for x in range(v//2)]
def kraj():
root.destroy()
kraj= tk.Tk(padx=170,pady=70)
kraj.title("Kraj")
tk.Button(kraj, text = 'reset', command =????).grid(row=0,column=0)
def vrati_karirano(n):
print ('some commands')
if igrace == []:
kraj()
def klik(n):
kartica[n].config(image = s[popis[n]])
if pr[0]:
if n!=okrenute[0]:
okrenute[1]=n
for i in range (v):
kartica[i].config(state=tk.DISABLED)
kartica[okrenute[0]].config(state=tk.NORMAL)
kartica[okrenute[1]].config(state=tk.NORMAL)
kartica[n].after(700, vrati_karirano, n)
kontrola[0]=False
pr[0]=False
elif kontrola[0]:
okrenute[0]=n
pr[0]=True
root = tk.Tk()
frame1 = tk.Frame(root)
frame1.pack(side = tk.TOP, fill = tk.X)
karirano = tk.PhotoImage(file="kari.GIF")
s = [tk.PhotoImage(file = "{}.GIF".format(i+1)) for i in range(9)]
for i in range(int(sqrt(v))):
for j in range(int(sqrt(v))):
gumb = tk.Button(frame1, image = karirano,command = partial(klik, b))
gumb.grid(row = i, column = j)
kartica.append(gumb)
b += 1
root.mainloop()
Since the only difference is the image on the button, you would reset the image for each button in "gumb" to "karirano".
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.