Noelia
Noelia

Reputation: 41

tkinter use same widget multple times

Since no one in the Spanish StakOverFlow hasn't answered me yet, I'm asking here. I'm from ARG. I am working on an mass document upload automation. The first widget I made with tkinter asks the user what type of document wants to upload to the web. Once this process is done, I want to throw another widget to ask the same question. The thing is I don't know how to write that code. I have not learned yet how to handle classes. And the code for my widget is a copy from an example of the web, and formatted to fit my specs.

from Tkinter import Tk, Label, Button
class DocumentTypeOption:
def __init__(self, master):
    self.master = master
    master.iconbitmap("bigpython.ico")
    master.minsize(280,150)
    master.geometry("280x150")
    master.title("DOCUMENT TYPE")

    self.label = Label(master, text="SELECT THE DOCUMENT TYPE")
    self.label.pack()

    self.tipo1_button = Button(master, text="Tipo1", command=self.opcion_tipo1)
    self.tipo1_button.pack()

    self.tipo2_button = Button(master, text="Tipo2", command=self.opcion_tipo2)
    self.tipo2_button.pack()
def funciontipo1(self):
    def subirtipo1():
        "things to upload doc type1"
    time.sleep(0.5)
    root.destroy()    
    time.sleep(1)
    subirtipo1()
    "SHOULD THE WIDGET BE CALLED HERE?"
def funciontipo2(self):
    def subirtipo1():
        "things to upload doc type2"
    time.sleep(0.5)
    root.destroy()    
    time.sleep(1)
    subirtipo2()
    "SHOULD THE WIDGET BE CALLED HERE?""
root = Tk()
my_gui = OpcionTipoDeDocumento(root)
root.mainloop()

When one type of document was uploaded I need to throw the widget once more in order to ask the user if he wants to continue with the other type of document.

Upvotes: 2

Views: 86

Answers (1)

Mike - SMT
Mike - SMT

Reputation: 15226

There are a few options. You could simply keep the Tkinter window open and ask the user if they want to load another file. You also are using sleep() inside a tkinter instance. You cannot use sleep() within Tkinter. There is another method called after() that is for setting up timed events to replace the use of sleep(). In this case I don't think you need a delay anyway.

Here is a simple example using a tkinter class and 1 function for the doc and 1 function to ask if you want to load another one.

# import tkinter as tk # for python 3.X
# from tkinter import messagebox # for python 3.X
import Tkinter as tk
import tkMessageBox as messagebox

class DocumentTypeOption(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.iconbitmap("bigpython.ico")
        self.minsize(280,150)
        self.geometry("280x150")
        self.title("DOCUMENT TYPE")

        self.label = tk.Label(self, text="SELECT THE DOCUMENT TYPE")
        self.label.pack()

        self.tipo1_button = tk.Button(self, text="Tipo1", command=lambda: self.do_stuff("Tipo1"))
        self.tipo1_button.pack()

        self.tipo2_button = tk.Button(self, text="Tipo2", command=lambda: self.do_stuff("Tipo2"))
        self.tipo2_button.pack()

    def do_stuff(self, doc_type):
        # things to upload doc type1
        # you can do this part with a single function as long as you check the doc type first.
        print(doc_type) # just to verify buttons are working.
        self.check_next(doc_type)

    def check_next(self, doc_type):
        x = messagebox.askyesno("DOCUMENT OPTION", "Would you like to load another {}?".format(doc_type))
        if x != True:
            self.destroy()


my_gui = DocumentTypeOption()
my_gui.mainloop()

Upvotes: 2

Related Questions