0

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Jon
  • 113
  • 1
  • 1
  • 10
  • 1
    No font, no text ! http://stackoverflow.com/questions/4072150/how-to-change-a-widgets-font-style-without-knowing-the-widgets-font-family-siz – dsgdfg Jul 21 '16 at 09:32
  • Did you find the solution yet? If not I might look into it today... – Ohumeronen Jul 21 '16 at 11:07
  • 1
    no solution yet. I have found a workaround - if I have the exit button in one column instead of trying to span across the text column too it seems to work. – Jon Jul 21 '16 at 14:17
  • it definitely doesnt depend on the font - if font is not specified, it just uses the default font. – Jon Jul 21 '16 at 14:19

3 Answers3

2

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.

Jon
  • 113
  • 1
  • 1
  • 10
0

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)
Parviz Karimli
  • 1,247
  • 1
  • 14
  • 26
0

The Basic structure for Button in Tkinter is

button1=Button(root,text="This is text ",command=functionname)
button1.pack()
MSA msa
  • 83
  • 1
  • 10