Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106783 > unrolled thread

Change Windows Tkinter after some time

Started byDiego Lelis <diego.o.lelis@gmail.com>
First post2016-04-10 06:36 -0700
Last post2016-04-10 15:13 -0400
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Change Windows Tkinter after some time Diego Lelis <diego.o.lelis@gmail.com> - 2016-04-10 06:36 -0700
    Re: Change Windows Tkinter after some time Terry Reedy <tjreedy@udel.edu> - 2016-04-10 14:11 -0400
    Re: Change Windows Tkinter after some time Terry Reedy <tjreedy@udel.edu> - 2016-04-10 15:13 -0400

#106783 — Change Windows Tkinter after some time

FromDiego Lelis <diego.o.lelis@gmail.com>
Date2016-04-10 06:36 -0700
SubjectChange Windows Tkinter after some time
Message-ID<1a13aefe-c66e-412a-8e9d-9afb684153d4@googlegroups.com>
I need to make a change between windows, after some time. But i am have a little bit of trouble making my code work: My windows change only when i click on button, i tried to put lambda in my command and also don't work.

import tkinter as tk   # python3
#import Tkinter as tk   # python
import datetime
TITLE_FONT = ("Helvetica", 18, "bold")
time_start = datetime.datetime.now()
delta_time = datetime.timedelta(seconds = 5)
class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        for F in (StartPage, TimePage):
            page_name = F.__name__
            frame = F(container, self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):
    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Time Page",
                            command=lambda: controller.show_frame(self.start_Counting()) )
        button1.pack()

    def start_Counting(self):
        global time_start
        time_start = datetime.datetime.now()
        return 'TimePage'

class TimePage(tk.Frame):

    def __init__(self, parent, controller):
        global delta_time
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is Time Page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        self.time_exit = tk.StringVar()
        self.time_exit.set('%s' %datetime.datetime.now())
        label2 = tk.Label(self, text=self.time_exit, font=TITLE_FONT)
        label2.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

    def update_Page(self):
        global time_start, delta_time
        #print('Executou o atualizar')
        time_until = delta_time - (datetime.datetime.now() - time_start)
        self.time_exit.set('%s' %time_until)
        if time_until <= 0:
            self.controller.show_frame('StartPage') # Go to the start_page after 5 seconds
        self.after(1000, update_Page)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

[toc] | [next] | [standalone]


#106796

FromTerry Reedy <tjreedy@udel.edu>
Date2016-04-10 14:11 -0400
Message-ID<mailman.11.1460311891.6211.python-list@python.org>
In reply to#106783
On 4/10/2016 9:36 AM, Diego Lelis wrote:
> I need to make a change between windows, after some time. But i am
> have a little bit of trouble making my code work: My windows change
> only when i click on button, i tried to put lambda in my command and
> also don't work.

I and others have written multiple answers on Stackoverflow about making 
root.after work.  I suggest that you search "[tkinter] root.after" there.

[snip somewhat baroque code]

I urge you to read SO's "How to create a Minimal, Complete, and 
Verifiable example" https://stackoverflow.com/help/mcve
This not only helps you write questions that get answers, but may help 
you find the problem yourself.

I believe that all the stuff with .now and datetime deltas is 
inessential noise. The initial parameter to .after is a integer 
millisecond delta. All the font stuff is irrelevant to the problem, so 
is some other stuff in your code.

I think you problem is that you never call root.after to start the 
process going.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#106797

FromTerry Reedy <tjreedy@udel.edu>
Date2016-04-10 15:13 -0400
Message-ID<mailman.12.1460315642.6211.python-list@python.org>
In reply to#106783
On 4/10/2016 2:11 PM, Terry Reedy wrote:
> On 4/10/2016 9:36 AM, Diego Lelis wrote:
>> I need to make a change between windows, after some time. But i am
>> have a little bit of trouble making my code work: My windows change
>> only when i click on button, i tried to put lambda in my command and
>> also don't work.
>
> I and others have written multiple answers on Stackoverflow about making
> root.after work.  I suggest that you search "[tkinter] root.after" there.
>
> [snip somewhat baroque code]
>
> I urge you to read SO's "How to create a Minimal, Complete, and
> Verifiable example" https://stackoverflow.com/help/mcve
> This not only helps you write questions that get answers, but may help
> you find the problem yourself.
>
> I believe that all the stuff with .now and datetime deltas is
> inessential noise. The initial parameter to .after is a integer
> millisecond delta. All the font stuff is irrelevant to the problem, so
> is some other stuff in your code.
>
> I think you problem is that you never call root.after to start the
> process going.

I see you also posted on SO and got essentially the same answer 4 hours 
before I wasted my time saying the same thing because I thought you had 
been ignored.  Please don't do this.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web