简体   繁体   中英

Python Tkinter deleting entry in function

I'm trying to make an app to show the time in different time zones. I've tried to make messagebox errors for if the entry is not in pytz.all_timezones and also if the user has entered something which doesn't contain a comma. The app seems to work until I begin to delete the entry, and it comes up with a messagebox which I don't want. Also, how can I let the time be unaffected by the entry changing until I press the search button again, because as soon as I begin to delete the entry the time stops updating.

Thanks

import tkinter as tk
from tkinter import *
from tkinter import messagebox
from datetime import datetime
import pytz
import time

root=tk.Tk()
root.title('Time app')


def times():
    a = entry_1.get()

    try:

        b = a.index(',')
        c = a[b + 2:] + '/' + a[:b]

        if c in pytz.all_timezones:
            home = pytz.timezone(c)
            local_time = datetime.now(home)
            current_time = local_time.strftime('%H:%M:%S')

            place_lbl = Label(root, text=a, bg='grey', width=15, font=('bold', 25))
            place_lbl.place(relx=0.33, rely=0.4)
            time_lbl = Label(root, text=current_time, bg='grey', font=('bold', 30))
            time_lbl.place(relx=0.41, rely=0.5)
            time_lbl.after(200,times)
           

        else:
            messagebox.showerror('Error',"Cannot find '{}'. Please enter in form city, continent (e.g. London, Europe).".format(a))


    except:
        messagebox.showerror('Error',"Cannot find '{}'. Please enter in form city, continent (e.g. London, Europe).".format(a))

    entry_1.delete(first=0, last=20)


canvas=tk.Canvas(root,height=400,width=700,bg='grey')
canvas.grid()

header_lbl=Label(root,text='Enter a city: ',bg='grey',fg='black',font=('bold',25))
header_lbl.place(relx=0.41,rely=0.1)

entry_1=Entry(root)
entry_1.place(relx=0.37,rely=0.2)

search_btn=Button(root,text='Search',command=times)
search_btn.place(relx=0.47,rely=0.3)

root.mainloop()

Here is how I fixed this issue, there might be some other way too.

def value():
    global a
    a = entry_1.get()

def times():
    try:
        b = a.index(',')
        c = a[b + 2:] + '/' + a[:b]
        if c in pytz.all_timezones:
            home = pytz.timezone(c)
            local_time = datetime.now(home)
            current_time = local_time.strftime('%H:%M:%S')

            place_lbl = Label(root, text=a, bg='grey', width=15, font=('bold', 25))
            place_lbl.place(relx=0.33, rely=0.4)
            time_lbl = Label(root, text=current_time, bg='grey', font=('bold', 30))
            time_lbl.place(relx=0.41, rely=0.5)
            time_lbl.after(1000,times)
        else:
            messagebox.showerror('Error',"Cannot find '{}'. Please enter in form city, continent (e.g. London, Europe).".format(a))
    except:
        messagebox.showerror('Error',"Cannot find '{}'. Please enter in form city, continent (e.g. London, Europe).".format(a))

Make sure to change your button to:

search_btn = Button(root,text='Search',command=lambda:[value(),times(),entry_1.delete(0, END)])

The problem is that your using after() but once the code is run, the entry box is empty and then onward you will be using the deleted value inside the box. So I passed in the value initially to another function, so that it will get stored there.

With the command of the button, I'm calling in three functions, first one is the value() which will have the value from the entry widget, then times() and the next is the deletion of the entry widget. If you include the deletion inside the times() then it would cause the entry to be deleted each second.

This works when you want to search more cities too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM