954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Tkinter Restart program

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

Vkitor
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

and how do I putt call to main loop?

Vkitor
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

I do not understand, you can not have tkinter program without starting mainloop.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

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()
Vkitor
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Since the only difference is the image on the button, you would reset the image for each button in "gumb" to "karirano".

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You