简体   繁体   English

为什么入口小部件在这种情况下不起作用我把它放在一个框架中,tkinter-python

[英]Why does the entry widget not work in this case i put it in a frame, tkinter-python

So this is a basic clock and alarm that i am creating, and in the process i want the user to type in what hour and minute they want to set for the alarm.所以这是我正在创建的基本时钟和闹钟,在此过程中,我希望用户输入他们想要为闹钟设置的小时和分钟。 But the entry widget here is not responding.但是这里的条目小部件没有响应。

import time 
import tkinter as tk


current_date, current_time = 0, 0
def current_info(timeinfo): #function to get the current time and date
    global current_date, current_time
    # current time
    current_time = time.strftime('%H:%M:%S')
    current_date = time.strftime(r'%m/%d/%Y')
    clock.after(200, timeinfo)

#Initialise the window
clock = tk.Tk()
clock.title('Easy CLock')
clock.configure(bg='#121212')
clock.columnconfigure(0, weight = 1)
clock.columnconfigure(1, weight = 1)
clock.columnconfigure(2, weight = 1)
clock.columnconfigure(3, weight = 1)


border_effects = {
    "flat": tk.FLAT,
    "sunken": tk.SUNKEN,
    "raised": tk.RAISED,
    "groove": tk.GROOVE,
    "ridge": tk.RIDGE,
}

#Logo will be under the main parent
logo = tk.PhotoImage(file = r'C:\Users\User\VSC\Alarm\Logo1.png')
logo_size = logo.subsample(5)

#Time and Date function
def time_date():
    current_info(time_date)
    #Displays the time
    c_time = tk.Label(f_time, text = current_time, fg='white', bg='#121212', font=('Verdana', 30))
    c_date = tk.Label(f_time, text = current_date, font=('Verdana', 10), fg='white', bg='#121212')
    c_time.grid(column=0, row=0)
    c_date.grid(column=0, row=1)

   
#alarm button command
def alarm_func(): 
    current_info(alarm_func)
    c_time = tk.Label(f_alarm, text = current_time, fg='white', bg='#121212', font=('Verdana', 10))
    c_date = tk.Label(f_alarm, text = current_date, font=('Verdana', 10), fg='white', bg='#121212')
   
    def pressed_enter(): #Command for the enter button
        set_label = tk.Label(f_alarm, text = f'Alarm has been set for {time_set}', fg ='white', bg = '#121212', borderwidth = 1, relief = border_effects['sunken'])
        set_label.grid(column = 4, row = 0, sticky = 'W')
       
    # Set the time and date for the alarm
    set_time = tk.StringVar()
    alarm_entry = tk.Entry(clock, textvariable = set_time)
    set_time.set('H : M')
    time_set = alarm_entry.get()
    
    #label and entry to set alarm / Enter Button
    c_label = tk.Label(f_alarm, text = 'Set Alarm: ', font = ('Verdana', 10), fg= 'white', bg ='#121212' )
    alarm_enter = tk.Button(f_alarm, text = 'Enter', font = ('Verdana', 7),  width = 5, command = pressed_enter)

    #Pack the widgets
    c_time.grid(row = 0, column = 0)
    c_date.grid(column = 1 , row = 0)
    alarm_enter.grid(row = 2, column = 3)
    c_label.grid(row = 2, sticky = 'W')
    alarm_entry.grid(row = 2, column = 1)
    

    #configure the empty columns
    f_alarm.columnconfigure(2, minsize = 10)

def recall_frame(event):
    if event == f_alarm:
        event.grid_forget()
        f_time.grid(column=0, row =1, columnspan = 4, sticky = 'N')
    elif event == f_time:
        event.grid_forget()
        f_alarm.grid(column=0, row=1, columnspan = 4, sticky = 'W')

def back_func():
    pass

#Creating Frames
f_time = tk.Frame(clock)  #Clock Button
f_alarm = tk.Frame(clock) #Alarm Buttton

#configure the frames
f_time.configure(bg = '#121212')
f_alarm.configure(bg = '#121212')

#Setting label in the frame
f_lbl = tk.Label(clock, text= ' Simplistic Clock', image = logo_size, font=('Verdana', 30), fg='white', bg='#121212', compound = tk.LEFT, padx = 35) 
time_but = tk.Button(clock, text='Clock', command= lambda :[time_date(), recall_frame(f_alarm)], bg='#f39c12', relief = border_effects['ridge'], pady = 7)
alarm_but = tk.Button(clock, text = 'Alarm', command = lambda :[alarm_func(), recall_frame(f_time)], bg='#f39c12', relief = border_effects['ridge'], pady = 7)
quit_but = tk.Button(clock, text='Exit', command = clock.quit, bg='#f39c12', relief = border_effects['ridge'], pady = 7)
back_but = tk.Button(clock, text = 'Back ', command = back_func, bg='#f39c12', relief = border_effects['ridge'], pady = 7)

f_lbl.config(borderwidth = 4, relief = border_effects['sunken'])

#Putting it on the frames
f_lbl.grid(column = 0, row = 0, columnspan = 5, sticky = 'EW')
time_but.grid(column = 0, row = 3, sticky = 'EW')
alarm_but.grid(column = 1, row = 3, sticky = 'EW')
quit_but.grid(column = 3, row = 3, sticky = 'EW')
back_but.grid(column = 2, row = 3, sticky = 'EW')

clock.mainloop()

i tried testing an entry widget outside the frame and the entry widget was able to work, is it because the frame f_alarm is not looping constantly in the background?我尝试在框架外测试一个入口小部件并且入口小部件能够工作,是因为框架 f_alarm 没有在后台不断循环吗?

When someone clicks on your button which activates the pressed_enter() function, it will call that function again every time which will set the time to H:M and it will get that value as the set_time.get() is called immediately after.当有人点击你的按钮激活pressed_enter() function时,它每次都会再次调用function,这会将时间设置为H:M,它会得到该值,因为set_time.get()会在之后立即调用。

You're also creating a new Entry every time the button is being clicked because you put alarm_entry = tk.Entry(clock, textvariable=set_time) in there as well.每次单击按钮时,您也会创建一个新条目,因为您也将alarm_entry = tk.Entry(clock, textvariable=set_time)放在那里。 You should only put the set_time.get inside of that button so that it gets the value that is currently filled in into the Entry.您应该只将set_time.get放在该按钮内,以便它获取当前填充到条目中的值。 The other things like其他的东西比如

set_time = tk.StringVar()
alarm_entry = tk.Entry(clock, textvariable=set_time)
set_time.set('H : M') 

Should be put outside of that function so they don't get called every time someone clicks on the button.应该放在 function 之外,这样他们就不会在每次有人点击按钮时被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Tkinter框架画布,小部件条目大小调整 - Python tkinter frame canvas, widget Entry resize tkinter 中的 Entry 小部件如何工作? - how does the Entry widget in tkinter work? Tkinter Python:如何使用 Frame Widget 将我的标签彼此对齐? - Tkinter Python: How can I use the Frame Widget to put my labels in line with one another? 为什么 tkinter 条目小部件在使用多个 windows 时返回空刺,但似乎在单个 window 中工作? - why does tkinter Entry widget returns empty sting while using multiple windows but seems to work in a single window? 为什么&lt; <ListboxSelect> &gt;双击Entry小部件时,在Python的Tkinter触发器中虚假绑定? - Why does <<ListboxSelect>> binding in Python's Tkinter trigger spuriously when double clicking an Entry widget? 如何获得Tkinter条目小部件,使其与普通的python输入相同 - How can I get a Tkinter entry widget to work the same as a normal python input Python Tkinter Entry小部件用法 - Python tkinter Entry widget usage Python中的Tkinter-条目小部件不显示 - Tkinter in Python - Entry widget not displaying Python 中的 Tkinter Entry 小部件不可编辑 - Tkinter Entry widget in Python is uneditable 为什么 .bind() 方法不适用于 Tkinter 中的框架小部件? - Why doesn't the .bind() method work with a frame widget in Tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM