亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python Tkinter 刪除函數中的條目

Python Tkinter 刪除函數中的條目

PHP
忽然笑 2023-11-09 15:50:15
我正在嘗試制作一個應用程序來顯示不同時區的時間。如果條目不在 pytz.all_timezones 中,并且用戶輸入了不包含逗號的內容,我嘗試生成消息框錯誤。該應用程序似乎一直有效,直到我開始刪除該條目,并且它出現了一個我不想要的消息框。另外,如何讓時間不受條目更改的影響,直到再次按下搜索按鈕,因為一旦我開始刪除條目,時間就會停止更新。謝謝import tkinter as tkfrom tkinter import *from tkinter import messageboxfrom datetime import datetimeimport pytzimport timeroot=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()
查看完整描述

1 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

這是我解決這個問題的方法,可能還有其他方法。


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))

確保將按鈕更改為:


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

問題是您使用的after(),但一旦代碼運行,輸入框為空,然后您將使用框內刪除的值。因此,我最初將值傳遞給另一個函數,以便將其存儲在那里。


通過按鈕的命令,我調用三個函數,第一個函數value()將具有條目小部件中的值,然后下times()一個是刪除條目小部件。如果您將刪除包含在其中,times()那么它將導致該條目每秒被刪除。


當您也想搜索更多城市時,這很有效。



查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 163 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號