How to change button colour in python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would want to create a button with tkinter that when you click it then it changes colour. I konw that I can do it by deleting the button but is there any other possibilitis.

Well, there are a lot of options. You can use many GUI libraries like Kivy, Tkinter. But as i dont know what you are using, i am just gonna make a program in Tkinter.
here is the code -
# importing modules
from tkinter import *
import random
# making a window
root = Tk()
root.geometry("400x400")
# just for decoration or the Background Color
mainframe = Frame(root, bg="#121212", width=400, height=400)
mainframe.pack()
# the function changing color
def color_changing(buttonObject):
# randomizes the color in hexcode
r = lambda: random.randint(0, 255)
color = '#%02X%02X%02X' % (r(), r(), r())
# .config configures an Object in Tkinter
buttonObject.config(bg=color)
# making a button
button = Button(root, width=10, font=('Segoe UI', 32, "bold"), text="Click Me", command=lambda: color_changing(button))
# root in Button() is the place where the button has to be placed
# text, width, font, etc are all attributes
# command is a simple way to call a function
# placing it in a specific location
button.place(relx=0.5, rely=0.5, anchor=CENTER)
# making the window so it does not stop running till it is said to be
root.mainloop()

Related

How to show message in popup window while python script is running [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Is there any way to show the message in popup window that process is running when python script start and disappear the popup window when process is done. Thanks in advance
A very flexible method you could look into is creating a tkinter window:
https://docs.python.org/3/library/tkinter.html
import tkinter as tk
import time
def some_function():
print('Do stuff')
time.sleep(3)
if __name__ == '__main__':
# Create window
window = tk.Tk()
# Create label
label_var = tk.StringVar()
label_var.set('Program is running...')
label = tk.Label(window, textvariable=label_var)
label.pack()
# Update and show window once
window.update_idletasks()
window.update()
# Your function code
some_function()
# Get rid of window
window.destroy()
Edit: just saw someone answered with tkinter in the comments... will leave this here as an example

How can I detect if a user click a tkinter button? [duplicate]

This question already has answers here:
How to handle a click button event in python module tkinter
(2 answers)
Closed 2 years ago.
Here is my code!
///
from tkinter import *
import os
import pygame
os.system(‘clear’)
#Window Setup
root = Tk()
root.title(‘cottontail’)
root.geometry(‘800x600’)
frame = Frame(root)
title_screen = Label(root, text = “Choose your rabbits name!”)
title_screen.pack()
name = Text(root, width=10 , height=3)
name.pack()
confirm_name = Button(root, text= “Conirm?”, width = 5, height=3)
root.mainloop()
///
My objective is to take the input the user puts in the text box to make a label in a pygame window with that name. I figured that a button would be an easy way to confirm the name and open the pygame screen. If this makes any sense to you it would really be appreciated if you could help me. Hope you have a good night!
Thanks in advance!
I'm not familiar with tkinter as I'm a PyQt dev, but I can help with your question as the logic behind both of them are same.
You can make a function that is called by the button whenever its clicked, and then, inside that function definition, do whatever you want.
Here's the website that explains this in detail -
https://www.delftstack.com/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/

How can I create a text file that saves an entry from tkinter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is my current db.py file, it contains the tkinter code for creating the GUI:
import tkinter
import db
app = Tk()
app.geometry("450x300")
app.mainloop()
You can use a Entry widget with a linked variable to get the input from user. The content can then be retrieved from the variable and written to a file using file objects.
I like to use themed-tkinter(ttk). If you just starting with creating GUI, I suggest you read more about themed-tkinter here.
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
# StringVar has all the logic to store and update string values
content = tk.StringVar()
def callback():
# This function is invoked when button `submit` is clicked
with open('content.txt', 'w') as file:
file.write(content.get())
entry = ttk.Entry(root, textvariable=content).grid()
submit = ttk.Button(root, text='submit', command=callback).grid()
root.mainloop()
Edit: I worded my answer wrongly for which I appologize. Tkinter by itself is indeed robust and powerful. I found it more easier to use ttk in many cases and had a softcorner towards it.

My python tkinter program will only show the first three buttons [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The program is meant to show 7 buttons but only shows the first three. I would appreciate some help, I'm trying to make a catalog program that writes files and text in those files. It works without tkinter, but the buttons arent showing up. I wrote the seven buttons and they worked originally but they have since stopped working. I don't know the problem since this is my first time with tkinter.
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
root.geometry("500x500")
root.title("Catalog 2020")
button = tk.Button(frame,
text = "Make New File",
command=answer1)
button.pack(side=tk.LEFT)
button2 = tk.Button(frame,
text = "Edit File",
command = answer2)
button2.pack(side=tk.LEFT)
button3 = tk.Button(frame,
text = "Append Data",
command = answer3)
button3.pack(side=tk.LEFT)
button4 = tk.Button(frame,
text = "Read File",
command = answer4)
button4.pack(side=tk.LEFT)
button5 = tk.Button(frame,
text = "Delete File",
command = answer5)
button5.pack(side=tk.LEFT)
button6 = tk.Button(frame,
text = "Tell me a Joke",
command = answer6)
button6.pack(side=tk.LEFT)
buttonquit = tk.Button(frame,
text="QUIT",
fg="red",
command=quit)
buttonquit.pack
root.mainloop()
With root.geometry('500x500') your asking your window to be '500x500' , remove that line to fit all the window with the widgets. Also you forgot to say () with the pack of last button, like buttonquit.pack() or you wont see that button too.

How to play a sound sample on every tkinter.text input? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I need to play a typewriter key sample on every tkinter.text input.
I came across the Playsound module but I don't know how to listen to the inputs.
You would use a bind and setup a function to play the sound when the bind is triggered.
import tkinter as tk
def key(event):
print("pressed", repr(event.char))
# Play sound here
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.bind('<Key>', key)
root.mainloop()
Thanks, I've come up with a very similar solution tho its really really laggy. Im basically writing a simple typewriter emulator so the key sound is reproduced for each typed letter.
import tkinter as tk
from PIL import Image, ImageTk
from playsound import playsound
def key(event):
key = event.char
playsound("C:/Users/Isma/key1.mp3")
win = tk.Tk()
frame = tk.Frame(win, width=300, height=400)
frame.grid(row=1, column=0)
text = tk.Text(frame)
text.grid(row=0,column=0)
text.bind('<Key>',lambda a : key(a))
image = Image.open("C:/Users/Isma/swintec1.jpg")
photo = ImageTk.PhotoImage(image)
label = tk.Label(frame,image=photo)
label.image = photo
label.grid(row=3,column=0)
win.mainloop()

Categories

Resources