简体   繁体   中英

Python Tkinter buttons - no text

I'm having a bit of a play with tkinter buttons. Im wanting to insert some buttons into a clock script I have.

Inserting the button Exit (3rd line from bottom) inserts a button ok, and the button works, but it refuses to show any text on the button.

How can I show text on this button?

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

Sort of solved it. The button was showing text - it was just off the screen. Solved it by adjusting the width of the tkinter text window and the buttons.

The value of w ( clock.winfo_screenwidth() ) is too wide for a button width. It just slides the name of the button too much to the left. So change the width of the button to a smaller number (200), and add sticky=W to the grid , so it won't slide too much anymore. Meanwhile, the width of the button will cover the whole width of the parent window (as you wish). So here's what to replace:

Exit = Button(clock, text="Close Tkinter Window", width = 200, height = 1, command=quit).grid(row = 1, rowspan = 1, column = 1, columnspan = w, sticky=W)

The Basic structure for Button in Tkinter is

button1=Button(root,text="This is text ",command=functionname)
button1.pack()

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