We can play a lot with buttons. Not just with coordinates, but with size, colors and font types:
Interesting thing, to activate all those additional options we need to have blank button with PhotoImage() where width and height must be set:
common_img = PhotoImage(width = 1, height = 1)
For simplicity reasons we will play with one button but_1. There is some things here that must be done.
One of them is set image = common_img.
Width is set to 100 and height to 20.
Background color is defined with bg="lightgreen".
Foreground color is set with fg = "red".
Those colors are static. Once you press button, color can be different.
To set button background while it is pressed we will say activebackground = "white".
An active foreground is set with activeforeground = "orange".
For a font we will use System type, with font = ("System").
Don't forget to connect command= with function run_ping()
Until now our code for a button is this:
common_img = PhotoImage(width = 1, height = 1)
but_1 = Button(top_win, text="Ping Google", image = common_img,
width = 100, height = 20,
compound = "c", bg = "lightgreen", fg = "red",
activebackground = "white", activeforeground = "orange",
font = ("System"), command = run_ping)
Of course we need button position and mainloop, but we know that from previous tutorials.
This is full working source:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("500x500")
def run_ping():
os.system("ping google.com")
common_img = PhotoImage(width = 1, height = 1)
but_1 = Button(top_win, text="Ping Google", image = common_img,
width = 100, height = 20,
compound = "c", bg = "lightgreen", fg = "red",
activebackground = "white", activeforeground = "orange",
font = ("System"), command = run_ping)
but_1.place(x = 5, y = 5)
top_win.mainloop()
No comments:
Post a Comment