Answers for "add function to tkinter button"

15

Function to a button in tkinter

from tkinter import *
#Creating a win
win = Tk()
#Giving a Function To The Button
def btn1():
  print("I Don't Know Your Name")
#Creating The Button
button1 =  Button(win, text="Click Me To Print SomeThing", command=btn1)
#put on screen
button1.pack()
win.mainloop()
#NB:This programme Will Print Something In The Terminal
#Check My Profile To See How We Print On The Screen Or Type In Google "Tkinter Label"
Posted by: Guest on April-21-2020
0

add a button on tkinter

import tkinter as tk
import random


def color(ndex):
    button_label_list[ndex][1].config(bg="#%06x" % random.randint(0, 16777215))


def dump():
    global count, button_label_list
    button_label_list.append([tk.Button(frame, text="color", command=lambda x=count: color(x)),
                              tk.Label(frame, text="Color", bg="#" + ("%06x" % random.randint(0, 16777215)))])
    button_label_list[-1][0].grid(row=0, column=count, sticky='nsew')
    button_label_list[-1][1].grid(row=1, column=count, sticky='nsew')
    frame.columnconfigure(count, weight=1)
    count += 1


root = tk.Tk()
count = 0
button_label_list = []
root.title("Hello")
root.rowconfigure(1, weight=1)
root.columnconfigure(2, weight=1)

frame = tk.Frame(root)
frame.rowconfigure(1, weight=1)
frame.grid(row=0, column=2, sticky='nsew', rowspan=2)

tk.Button(root, text="butt ON", command=dump).grid(row=0, column=0, sticky='nsew')
tk.Button(root, text="Quit!", command=root.quit).grid(row=0, column=1, sticky='nsew')
tk.Label(root, text="This is a label", bg="PeachPuff").grid(row=1, column=1, columnspan=1, sticky='nsew')

root.mainloop()
Posted by: Guest on March-24-2022

Code answers related to "add function to tkinter button"

Python Answers by Framework

Browse Popular Code Answers by Language