03-tkinter-Button Learning Notes

Keywords: Python calculator

Button (parent object, options...)
options take the value borderwidth or bd: the border width defaults to two pixels; bg: background color; command: when you click a button, execute this method; cursor: the shape on which the mouse moves to the button; fg: foreground color.Font: font; height: high; highlightbackground: the background color when the button gets focus; highlightcolor: the color when the button gets focus; image: the image on the button; justify: when there are multiple lines of text, the alignment of the last line of text; padx/pady: default is 1, you can set the distance between the button and text and the distance between the top and bottom of the button; relief: defaultIs relief=FLAT, you can control the text outline; state: default is state=NORMAL, if set to DISABLED, the function button is displayed in gray scale, indicating temporary unavailability; text: the name of the button; underline: set the number of text underlined, from 0 onwards, the default-1 is no underline; width: width, the unit is character width; wraplength: limit the number of text per lineThe default is 0, meaning only \n will wrap lines.

32-Set a button that, when clicked, displays the string "I LIKE TKINTER". The background color is blue and the string color is white.

from tkinter import *
win=Tk()
win.geometry('400x500')
def show():
label['text']="I LIKE TKINTER"
label['bg']="blue"
label['fg']="white"
label=Label(win)
button=Button(win,text="Click me to print a message",command=show)
label.pack()
button.pack()
win.mainloop()

33-Set a window that can be closed by clicking Cancel.

from tkinter import *
win=Tk()
def show():
label.config(text="I LIKE TKINTER",bg="blue",fg="white")
label=Label(win)
label.pack()
button1=Button(win,text="Print message",command=show)
button2=Button(win,text="cancel",command=win.destroy)
button1.pack(side=LEFT)
button2.pack(side=LEFT)
win.mainloop()

34-Design a timer program, add an end button, when you click the end button, the program execution ends.

from tkinter import *
win=Tk()
counter=0
def run_counter(digit):
def counting():
global counter
counter+=1
digit.config(text=str(counter))
digit.after(1000,counting)
counting()
digit=Label(win,bg="yellow",fg="black",height=3,width=10,font="Song Style 20 bold")
digit.pack()
run_counter(digit)
Button(win,text="End",width=15,command=win.destroy).pack()
win.mainloop()

35-Set three buttons Click the blue button to set the window background color to blue, click the red button to set the window color to red, and click the exit button to exit the program.

from tkinter import *
win=Tk()
def red():
win.config(bg="red")
def blue():
win.config(bg="blue")
redbutton=Button(win,text="red",command=red)
bluebutton=Button(win,text="blue",command=blue)
exitbutton=Button(win,text="exit",command=win.destroy)
redbutton.pack(anchor=S,side=RIGHT,padx=5,pady=5)
bluebutton.pack(anchor=S,side=RIGHT,padx=5,pady=5)
exitbutton.pack(anchor=S,side=RIGHT,padx=5,pady=5)
win.mainloop()

Create buttons with images:

from tkinter import *
def showmessage():
    label.config(text="I like tkinter",bg="yellow",fg="black")
win=Tk()
label=Label(win)
imggif=PhotoImage(file=r"C:\Users\Administrator\Downloads\img.gif")
btn=Button(win,image=imggif,command=showmessage)
label.pack()
btn.pack()
win.mainloop()

Implement a simple calculator layout:

from tkinter import *
win=Tk()
label=Label(win,text="",bg="yellow",width=20)
label.grid(row=0,column=0,columnspan=4)
button1=Button(win,text="7",width=3)
button2=Button(win,text="8",width=3)
button3=Button(win,text="9",width=3)
button4=Button(win,text="*",width=3)
button5=Button(win,text="4",width=3)
button6=Button(win,text="5",width=3)
button7=Button(win,text="6",width=3)
button8=Button(win,text="-",width=3)
button9=Button(win,text="1",width=3)
button10=Button(win,text="2",width=3)
button11=Button(win,text="3",width=3)
button12=Button(win,text="+",width=3)
button13=Button(win,text="0",width=8)
button14=Button(win,text=".",width=3)
button15=Button(win,text="=",width=3)
button1.grid(row=1,column=0,padx=5)
button2.grid(row=1,column=1,padx=5)
button3.grid(row=1,column=2,padx=5)
button4.grid(row=1,column=3,padx=5)
button5.grid(row=2,column=0,padx=5)
button6.grid(row=2,column=1,padx=5)
button7.grid(row=2,column=2,padx=5)
button8.grid(row=2,column=3,padx=5)
button9.grid(row=3,column=0,padx=5)
button10.grid(row=3,column=1,padx=5)
button11.grid(row=3,column=2,padx=5)
button12.grid(row=3,column=3,padx=5)
button13.grid(row=4,column=0,padx=5,columnspan=2)
button14.grid(row=4,column=2,padx=5)
button15.grid(row=4,column=3,padx=5)
win.mainloop()

Posted by beginneratphp on Tue, 12 Nov 2019 12:31:54 -0800