Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

Python tkinter window seems to freeze when i drag it over a screen

I was trying to create simple Timer. While coding I've experienced problem with my program. When i drag the window over the screen it seems to be freezed - I could see that the timer stops although the label is constantly updating. I tried many ways to solve it but unsuccessfully i couldnt find the answer. I hope someone have it. Thank you in advance

Here is my full code :

import tkinter as tk
from tkinter.font import Font

class App:
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry('300x200')
        self.root.title('Simple Timer')
        self.font1 = Font(family='Arial',size=60)
        self.input_entry = tk.Entry(self.root,font=self.font1,bg='gray94',relief=tk.FLAT,cursor='xterm',justify=tk.CENTER)
        self.input_entry.insert(0, '3:00')
        self.display = tk.Label(self.root,text='test',font=self.font1,padx=70)
        self.input_entry.pack()
        self.btn_accept = tk.Button(self.root,text='Start',padx=15,pady=5,command=self.accept)
        self.btn_accept.pack()
        self.root.mainloop()

    def accept(self):
        self.data = ''.join(self.input_entry.get())
        self.display.configure(text=self.data)
        if ':' in self.data:
            self.index = self.data.find(':')
            self.minutes = int(self.data[0:self.index])
            self.seconds = int(self.data[self.index+1:len(self.data)])
            if self.seconds > 59:
                print('Error: wrong input')
            else:
                self.btn_accept["state"] = 'disabled'
                self.time_ = self.seconds + 60*self.minutes
                self.btn_accept.pack_forget()
                self.input_entry.pack_forget()
                self.display.pack()
                self.btn_accept.pack()
                for ms in range(0,self.time_+1):
                    self.root.after(1000)
                    if self.seconds != 0:
                        self.seconds -= 1
                    else:
                        if self.minutes > 0:
                            self.seconds = 59
                            self.minutes -= 1
                        else:
                            print('koniec czasu')
                    self.seconds_updated = self.seconds
                    if len(str(self.seconds)) == 1:
                        self.seconds_updated = '0'+str(self.seconds)

                    self.display.configure(text=(f'{self.minutes}:{self.seconds_updated}'))
                    self.root.update()
        else:
            print('Error: wrong input')

app = App()
question from:https://stackoverflow.com/questions/65947740/python-tkinter-window-seems-to-freeze-when-i-drag-it-over-a-screen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...