Question on how to stop my button click on tkinter? - python

Basically, I am making a really basic python game for school and i am just trying to rush finish this as fast as I can, after def RNG(): After I get the button to give me a random number, how do I stop the button for it? I can't spam click it.
import tkinter
from tkinter import *
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
root = Tk( )
app = Window(root)
root.title("BlackJack Game")
root.geometry("1920x1080+0+0")
root.configure(bg='gold')
lbl: Label = tkinter.Label(root, text='Welcome to Blackjack!', fg='steelblue', bg='gold1',
font=('arial', 50, "bold"))
lbl.pack()
def open():
global top
top = Toplevel()
top.configure(bg='green')
top.geometry("1920x1080+0+0")
top.title('Game Window')
lbl_1 = Label(top, text="BlackJack\n ", font=("arial", 75, "bold"), fg="black", bg="green").pack()
root.withdraw()
def RNG():
import random
x = random.randint(2, 21)
lab = Label(top, text=x, font=("arial", 20, "bold"), fg="Black", bg='brown')
lab.pack()
global but
but = Button(top, text='test', font=("arial", 20, "bold"), fg="Black", bg='brown', command=RNG, height=7, width=18).pack()
work = Button(root, text="Press Play: ", font=("arial", 20, "bold"), fg="Black", bg='orange', command=open, ).pack()
root.mainloop()

You can do this in a few ways.
First: You can set the button's state to disabled
but = Button(top, text='test', state = DISABLED, font=("arial", 20, "bold"), fg="Black", bg='brown', command=RNG, height=7, width=18).pack()
or
but['state'] = DISABLED
Note that you should be disabling the button in the function it is calling. (for simplicity sake)
Second: You can redefine the button after and change the command it calls to '" "'
but = Button(top, text='test', state = DISABLED, font=("arial", 20, "bold"), fg="Black", bg='brown', command="", height=7, width=18).pack()
that way it cannot be spammed, it also means that when you call open() it should change the but button back to normal.

Related

Program not running when double clicked but runs from cmd?

I have made a python gui using tkinter and i have been running it through the commandline. But when i tried to open it by double clicking, it shortly shows the window but exits again. I tried commenting out all the places where it uses images thinking that it is because it dosent have file-editing permissions when its run by double click and it worked! But i need to use images in the gui, anyone have solutions?
import tkinter as tk
from PIL import Image, ImageTk
import time
root = tk.Tk()
root.title("Krypto Coin")
root.iconbitmap('icon.ico')
root.configure(bg="#112233")
Frame = tk.Frame(root, width=1200, height=800, bg="#112233")
Frame.grid(columnspan=3, rowspan=5)
icon = Image.open('icon_round.png')
icon = ImageTk.PhotoImage(icon)
icon_label = tk.Label(image=icon, borderwidth=0, highlightthickness=0)
icon_label.place(relx=0.5, rely=0.2, anchor="center")
text = tk.Label(root, text="Welcome to Krypto Coin (KTC)", bg="#112233", fg="#ffffff", font=("Arial", 32))
text.place(relx=0.5, rely=0.45, anchor="center")
text = tk.Label(root, text="Enter id of the receiver and amount", bg="#112233", fg="#ffffff", font=("Arial", 24))
text.place(relx=0.5, rely=0.5, anchor="center")
def trasfer_button_pressed():
time.sleep(0.1)
trasfer_button_text.set("Loading...")
trasfer_button.configure(state="disable")
trasfer_button.configure(bg="#112233")
input_id.configure(state="disable")
input_amount.configure(state="disable")
def trasfer_button_enter(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#334455")
def trasfer_button_leave(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#223344")
trasfer_button_text = tk.StringVar()
trasfer_button = tk.Button(root, textvariable=trasfer_button_text, command=lambda:trasfer_button_pressed(), bg="#223344", fg="#ffffff", width=15, height=1, font=("Arial", 24), borderwidth=0, highlightthickness=0)
trasfer_button_text.set("Transfer")
trasfer_button.place(relx=0.5, rely=0.65, anchor="center")
trasfer_button.bind("<Enter>", trasfer_button_enter)
trasfer_button.bind("<Leave>", trasfer_button_leave)
input_id = tk.Entry(root, width=48, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_id.insert(0, "ID")
input_id.place(relx=0.45, rely=0.8, anchor="center", height=30)
input_amount = tk.Entry(root, width=20, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_amount.insert(0, "Amount")
input_amount.place(relx=0.65, rely=0.8, anchor="center", height=30)
root.mainloop()

Why is everything displayed in master window instead of particular frames?

So I started creating GUI that I'll use for testbench for electrical engines. My question is why is everything displayed in master window like this?:
Instead of the two frames?
The first time I wrote it, it was working perfectly:
[
but then I wanted to rearrange the code using classes and the problem appeared. I'm using Python 3.7.
My code:
import tkinter as tk
from tkinter import *
from tkinter import messagebox
class Lintebench():
def __init__(self, master):
self.master = master
self.master.title("Linte^2 testbench")
self.master.geometry('1000x600')
#FRAMES
frame = Frame(master, bg='#3e646c').place(relwidth=0.2, relheight=0.4, rely=0.6)
frame2 = Frame(master, bg='#3e646c').place(relwidth=0.8, relheight=0.4, rely=0.6, relx=0.2)
#FRAME 1
self.start_button = Button(frame, text="Start", padx=50, pady=50, bg='green', activebackground='grey', command=self.start_engine).pack()
self.stop_button = Button(frame2, text="Stop", padx=50, pady=50, bg='red', activebackground='grey', command=self.stop_engine).pack()
#FRAME 2
self.parameters = Label(frame2, text="PARAMETERS", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0)
self.set_parameter = Label(frame2, text="SET", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0.8)
self.values = Label(frame2, text="VALUES", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0.4)
self.torque = Label(frame2, text="TORQUE", font=("Arial", 12), fg='white', bg='#3e646c').place(relx=0.7, rely=0.2)
self.velocity = Label(frame2, text="VELOCITY", font=("Arial", 12), fg='white', bg='#3e646c').place(relx=0.7, rely=0.6)
#parameters to set
self.set_torque = Scale(frame2, orient=HORIZONTAL, length=200).place(relx=0.7, rely=0.3)
self.set_velocity = Scale(frame2, orient=HORIZONTAL, length=200).place(relx=0.7, rely=0.7)
def start_engine(*args):
messagebox.showinfo('Information','Engine was started')
def stop_engine(*args):
messagebox.showinfo('Information','Engine was stopped')
def main():
root = Tk()
lintebench = Lintebench(root)
root.mainloop()
if __name__ == '__main__':
main()here
It is because you have chained Frame(...) with place(...) and so frame and frame2 will be None. Separate the chained statement into two statements:
frame = Frame(master, bg='#3e646c')
frame.place(relwidth=0.2, relheight=0.4, rely=0.6)
frame2 = Frame(master, bg='#3e646c')
frame2.place(relwidth=0.8, relheight=0.4, rely=0.6, relx=0.2)
Actually you need to separate all the chained statements in your code if you want to reference the widgets in other place.
Also self.stop_button should be child of frame, not frame2:
self.stop_button = Button(frame, text="Stop", padx=50, pady=50, bg='red', activebackground='grey', command=self.stop_engine)
self.stop_button.pack()
I believe is becuase you're asigning their master as root, and you only have one root and when you make roo.mainloop() that makes them focus.

How do I delete window using a button

*So far in my code I show a couple of examples and then ask if the user is ready to take the quiz. I would like to make the window that has the examples to disappear once I press the button that says Yes but I was told I cannot use to commands in one button. What would be the best way to delete the window? *
from tkinter import *
root= Tk()
def clickEvent1():
master = Tk()
mainloop()
def clickEvent2():
master = Tk()
Label2=Label(master, text="Here are the scientific names of some Animals", font=("Times", 13) )
Label2.pack()
Label3=Label(master, text="Wolf=Canis lupus", font=("Arial", 11))
Label3.pack()
Label4=Label(master, text="Lion=Panthera leo",font=("Arial", 11))
Label4.pack()
Label5=Label(master, text="Panda=Ailuropoda melanoleuca", font=("Arial", 11))
Label5.pack()
Label6=Label(master, text="Jellyfish=Medusozoa", font=("Arial", 11))
Label6.pack()
Label7=Label(master, text="Marmoset=Callithrix jacchus", font=("Arial", 11))
Label7.pack()
Label8=Label(master, text="Tiger=Panthera tigris", font=("Arial", 11))
Label8.pack()
Label9=Label(master, text="Zebra=Equus quagga", font=("Arial", 11))
Label9.pack()
Label0=Label(master, text="Would you like to take the quiz now?", font=("Arial", 13))
Label0.pack()
button3=Button(master, text="Yes", font=("Arial", 12), fg="White",bg="Turquoise",height=1, width=10, command=clickEvent1 )
button3.pack()
mainloop()
The code above is where the yes button is used and where the command to make a new window is called.
mylabel=Label(root, text="Welcome to the Animal Trivia Game!", font=("Arial", 14))
mylabel.pack()
label2=Label(root, text="Click 'Start' to begin:)", font=("Arial", 14))
label2.pack()
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
leftFrame=Frame(root)
leftFrame.pack(side=LEFT)
rightFrame=Frame(root)
rightFrame.pack(side=RIGHT)
button1=Button(topFrame,text="Start", font=("Arial", 16), fg="White",bg="Turquoise",height=1, width=10,command=clickEvent2)
button1.pack()
mainloop()

I am trying to delete a window using a button.

I am trying to delete the previous window I created but once I use a command to call it, I receive an error. Can anyone care to explain how this works?
from tkinter import *
root= Tk()
I need help here.
def clickEvent():
master = Tk()
Label2=Label(master, text="Here are the scientific names of some Animals", font=("Times", 13) ).grid(row=0)
Label(master, text="Wolf=Canis lupus", font=("Arial", 11)).grid(row=1)
Label(master, text="Lion=Panthera leo",font=("Arial", 11)).grid(row=2)
Label(master, text="Panda=Ailuropoda melanoleuca", font=("Arial", 11)).grid(row=3)
Label(master, text="Jellyfish=Medusozoa", font=("Arial", 11)).grid(row=4)
Label(master, text="Marmoset=Callithrix jacchus", font=("Arial", 11)).grid(row=5)
Label(master, text="Tiger=Panthera tigris", font=("Arial", 11)).grid(row=6)
Label(master, text="Zebra=Equus quagga", font=("Arial", 11)).grid(row=7)
Label(master, text="Would you like to take the quiz now?", font=("Arial", 13)).grid(row=15)
e1 = Entry(master,textvariable=entryText).grid(row=1, column=1)
button2 = Button(root, text="Yes", command=create_window)
button2.pack()
mylabel=Label(root, text="Welcome to the Animal Trivia Game!", font=("Arial", 14))
mylabel.pack()
label2=Label(root, text="Click 'Start' to begin:)", font=("Arial", 14))
label2.pack()
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
leftFrame=Frame(root)
leftFrame.pack(side=LEFT)
rightFrame=Frame(root)
rightFrame.pack(side=RIGHT)
button1=Button(topFrame,text="Start", font=("Arial", 16), fg="White",bg="Turquoise",height=1, width=10,command=clickEvent)
button1.pack()
mainloop()
def clickEvent2():
master=Tk()
mainloop()
def clickEvent2():
master = Tk()
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
leftFrame=Frame(root)
leftFrame.pack(side=LEFT)
rightFrame=Frame(root)
rightFrame.pack(side=RIGHT)
button2=Button(topFrame,text="Start Now", font=("Arial", 16), fg="White",bg="Turquoise",height=1, width=10,command=clickEvent)
button2.pack()
mainloop()
root.mainloop()
greatly appreciate it if anyone could help.
You should never have more than one Tk and Mainloop, it will cause many problems in your code. You will have to use a Toplevel instead, which is basically also a new window. To delete a Toplevel, use it's .destroy() method. While to hide it, use it's .withdraw() method.

Changing part of a message's color in tkinter messagebox

I have a TKinter messagebox like the one below. I would like to change part of the message to a different color. For example in the messagebox below I would like the language to be Blue. Is this possible?
It's not possible to change such options of Tkinter Standard Dialogs. You need to create your own dialog. You'll also need to separate the text parts. I've tried to make something similar in the image that the OP has posted above:
from tkinter import *
root = Tk()
def choosefunc(option):
if option == "cancel":
print("Cancel choosen")
else:
print("OK choosen")
def popupfunc():
tl = Toplevel(root)
tl.title("Languages")
frame = Frame(tl)
frame.grid()
canvas = Canvas(frame, width=100, height=130)
canvas.grid(row=1, column=0)
imgvar = PhotoImage(file="pyrocket.png")
canvas.create_image(50,70, image=imgvar)
canvas.image = imgvar
msgbody1 = Label(frame, text="The", font=("Times New Roman", 20, "bold"))
msgbody1.grid(row=1, column=1, sticky=N)
lang = Label(frame, text="language(s)", font=("Times New Roman", 20, "bold"), fg='blue')
lang.grid(row=1, column=2, sticky=N)
msgbody2 = Label(frame, text="of this country is: Arabic", font=("Times New Roman", 20, "bold"))
msgbody2.grid(row=1, column=3, sticky=N)
cancelbttn = Button(frame, text="Cancel", command=lambda: choosefunc("cancel"), width=10)
cancelbttn.grid(row=2, column=3)
okbttn = Button(frame, text="OK", command=lambda: choosefunc("ok"), width=10)
okbttn.grid(row=2, column=4)
label = Label(root, text="Click to proceed:")
label.grid()
button = Button(root, text="Click", command=popupfunc)
button.grid()
(Image URL: http://imgur.com/a/Nf75v)

Categories

Resources