give utton a number python code example

Example 1: how to make a button in python

import tkinter as tk
    

def write_slogan():
    print("Tkinter is easy to use!")

root = tk.Tk()
frame = tk.Frame(root)
frame.pack()

button = tk.Button(frame, 
                   text="QUIT", 
                   fg="red",
                   command=quit)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
                   text="Hello",
                   command=write_slogan)
slogan.pack(side=tk.LEFT)

root.mainloop()

Example 2: how to create a button python

import Tkinter
import tkMessageBox

top = Tkinter.Tk()

def helloCallBack():
   tkMessageBox.showinfo( "Hello Python", "Hello World")

B = Tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack()
top.mainloop()

Example 3: give utton a number python

def create_buttons(self):
    for x in range(10):
        button = ttk.Button(self, text=x, command=lambda number=x: self.callback(number))
        button.grid()

Example 4: give utton a number python

def callback(self, number):
    new_value = self.user_input.get() + str(number)
    self.user_input.set(new_value)

Tags:

Misc Example