我正在尝试使用tkinter按钮。我想在我的时钟脚本中插入一些按钮。
插入按钮退出(从底部第3行)插入按钮确定,按钮有效,但它拒绝在按钮上显示任何文字。
如何在此按钮上显示文字?
import sys
if sys.version_info[0] == 2:
from Tkinter import *
import Tkinter as tk
else:
from tkinter import *
import tkinter as tk
from time import *
fontsize=75
fontname="Comic Sans MS" #font name - use Fontlist script for names
fontweight="bold" #"bold" for bold, "normal" for normal
fontslant="roman" #"roman" for normal, "italic" for italics
def quit():
clock.destroy()
def getTime():
day = strftime("%A")
date = strftime("%d %B %Y")
time = strftime("%I:%M:%S %p")
text.delete('1.0', END) #delete everything
text.insert(INSERT, '\n','mid')
text.insert(INSERT, day + '\n', 'mid') #insert new time and new line
text.insert(INSERT, date + '\n', 'mid')
text.insert(INSERT, time + '\n', 'mid')
clock.after(900, getTime) #wait 0.5 sec and go again
clock = tk.Tk() # make it cover the entire screen
w= clock.winfo_screenwidth()
h= clock.winfo_screenheight()
clock.overrideredirect(1)
clock.geometry("%dx%d+0+0" % (w, h))
clock.focus_set() # <-- move focus to this widget
clock.bind("<Escape>", lambda e: e.widget.quit())
text = Text(clock, font=(fontname, fontsize, fontweight, fontslant))
text.grid(column = 1, columnspan = 1, row = 2, rowspan = 1, sticky='')
Exit = Button(clock, text="Close Tkinter Window", width = w, height = 1, command=quit).grid(row = 1, rowspan = 1, column = 1, columnspan = w)
clock.after(900, getTime)
clock.mainloop()
答案 0 :(得分:2)
排序解决了它。按钮显示文本 - 它刚刚离开屏幕。通过调整tkinter文本窗口和按钮的宽度来解决它。
答案 1 :(得分:0)
w
(clock.winfo_screenwidth()
)的值对于按钮宽度而言太宽。它只是将按钮的名称滑动到左侧太多。因此,将按钮的宽度更改为较小的数字(200),并将sticky=W
添加到grid
,这样它就不会再滑动太多了。同时,按钮的宽度将覆盖父窗口的整个宽度(如您所愿)。所以这是替换的内容:
Exit = Button(clock, text="Close Tkinter Window", width = 200, height = 1, command=quit).grid(row = 1, rowspan = 1, column = 1, columnspan = w, sticky=W)
答案 2 :(得分:0)
Tkinter中Button的基本结构是
button1=Button(root,text="This is text ",command=functionname)
button1.pack()