首页 > 解决方案 > 为什么按下我的 tkinter python 时钟的按钮时会有空格

问题描述

这是我刚入手的时钟,我想做的是让按钮具备闹钟和时钟等各项功能。我已经这样做了,但是当我按下按钮时,会出现空白,并且标题会向右移动并有一点缩进。总的来说,这个设计太奇怪了,因为我刚开始接触 tkinter,我对框架的了解不多。

import time
import tkinter as tk

#Initialise the window
clock = tk.Tk()
clock.title('Easy CLock')
clock.geometry('400x700')
clock.configure(bg='#121212')

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

#Time and Date function
def time_date():
    # current time
    current_time = time.strftime('%H:%M:%S')
    current_date = time.strftime(r'%m/%d/%Y')
    clock.after(200, 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=1)
    c_date.grid(column=0, row=2)

#alarm button command
def alarm_func():
    c_clicked = tk.Label(text='Alarm Interface', fg='white', bg='#121212')
    c_clicked.grid(column=0, row=1)


#Creating Frames
f_header = tk.Frame(clock) #Header
f_time = tk.Frame(clock)  #Clock Button
f_alarm = tk.Frame(clock) #Alarm Buttton
f_exit = tk.Frame(clock) #Exit button

#Setting the Frames with grid
f_header.grid(column=0 , row=0, columnspan = 3)
f_time.grid(column=0, row =3)
f_alarm.grid(column=1, row=3)
f_exit.grid(column=2, row =3)

#Setting label in the frame
f_lbl = tk.Label(f_header, text='Simplistic Clock', font=('Verdana', 30),fg='white', bg='#121212') # Purple text
time_but = tk.Button(f_time, text='Clock', command= time_date, bg='#f39c12', width = 15, relief = border_effects['ridge'])
alarm_but = tk.Button(f_alarm, text = 'Alarm', command = alarm_func, bg='#f39c12', width = 15, relief = border_effects['ridge'])
quit_but = tk.Button(f_exit, text='Exit', command = clock.quit, bg='#f39c12', width = 15, relief = border_effects['ridge'])



#Putting it on the frames
f_lbl.grid()
time_but.grid()
alarm_but.grid()
quit_but.grid()


clock.mainloop()

另外,如果按下闹钟按钮,我想隐藏时钟,例如,如果用户先按下时钟按钮,它将显示代码,但是当用户决定按下闹钟按钮时,我希望隐藏时钟框架。我厌倦了用户 .grid_forget() 但它对我不起作用。我不确定框架和网格是如何工作的。

框架是否有自己的列和网格,因为在每个框架中,开始将是 column = 0,row = 0 还是跟随父窗口。

标签: pythontkinterclock

解决方案


推荐阅读