Related
I have built 7 individual calculators:
Basic Calculator
Scientific Calculator
Bitwise Calculator
Binary → All Calculator
Decimal → All
Octal → All
Hexadecimal → All
works very fine INDIVIDUALLY
In order to open each program, I've created a floating window that connects all programs in one.
But when I open each calculator from there the CMD opens.
import os
import tkinter as tk
from tkinter import *
from tkinter import messagebox
if __name__ == '__main__':
o = Tk()
menu = Menu(o)
new = Menu(menu, tearoff=0)
menu.add_cascade(label='File', menu=new)
def new_window():
os.system('Calculator.exe')
new.add_command(label='New', command=new_window)
about = Menu(menu, tearoff=0)
menu.add_cascade(label='About', menu=about)
about.add_command(label='Version 1.0', command=None)
def ExitApplication():
msg_box = tk.messagebox.askquestion('Exit Application', 'Are you sure you want to exit the application',
icon='warning')
if msg_box == 'yes':
o.destroy()
else:
tk.messagebox.showinfo('Return', 'Return to the application window')
exit_click = Menu(menu, tearoff=0)
menu.add_cascade(label='More', menu=exit_click)
exit_click.add_command(label='Exit', command=ExitApplication)
o.config(menu=menu)
o.title("Calculator Options")
o.iconphoto(True, PhotoImage(file="calculator.png"))
o.geometry("500x521")
o.config(relief=SUNKEN, bg="#ADD8E6")
def click_basic():
os.system('Basic_Calculator.exe')
def click_scientific():
os.system('Scientific_Calculator.exe')
def click_binary_to_all():
os.system('Binary_to_All.exe')
def click_octal_to_all():
os.system('Oct_to_All.exe')
def click_decimal_to_all():
os.system('Dec_to_All.exe')
def click_hex_to_all():
os.system('Hex_to_All.exe')
def click_bitwise():
os.system('Bitwise_Operations.exe')
def button():
Button(o, bg="#BCD4E6", fg="black", relief="groove", text="Basic", font=("Times New Roman", 16, "bold"),
command=click_basic).pack(fill="both", expand=True)
Button(o, bg="#89CFF0", fg="black", relief="groove", text="Scientific", font=("Times New Roman", 16, "bold"),
command=click_scientific).pack(fill='both', expand=True)
Button(o, bg="#BCD4E6", fg="black", relief="groove", text="Bin → Oct, Dec, Hex", font=("Times New Roman", 16, "bold"),
command=click_binary_to_all).pack(fill='both', expand=True)
Button(o, bg="#89CFF0", fg="black", relief="groove", text="Oct → Bin, Dec, Hex", font=("Times New Roman", 16, "bold"),
command=click_octal_to_all).pack(fill='both', expand=True)
Button(o, bg="#BCD4E6", fg="black", relief="groove", text="Dec → Bin, Oct, Hex",
font=("Times New Roman", 16, "bold"), command=click_decimal_to_all).pack(fill='both', expand=True)
Button(o, bg="#89CFF0", fg="black", relief="groove", text="Hex → Bin, Oct, Dec",
font=("Times New Roman", 16, "bold"), command=click_hex_to_all).pack(fill='both', expand=True)
Button(o, bg="#BCD4E6", fg="black", relief="groove", text="Bitwise Operations",
font=("Times New Roman", 16, "bold"), command=click_bitwise).pack(fill='both', expand=True)
button()
o.mainloop()
This is my floating window code.
I've embedded an example too.
Clicking on the Basic option opens the Basic Calculator fine but also opens the CMD disabling the Floating Window https://i.stack.imgur.com/MYGmf.png
im lost!
im trying to write a some sort of a blackjack game with a few changes from the original game.
if you notice in towards the end of the code i made a button named restart_button.
the function he calls is reset_game. when i try to run the code i get the error message - NameError: name 'reset_game' is not defined
do you guys have any idea of how do i fix it? thanks in advance
ps. i started coding like 2 months ago, so please dont get mad at my not so pretty code.
import random
from tkinter import *
window = Tk()
window.title("Black Jack (made by ziv)")
icon = PhotoImage(file='icon.png')
window.iconphoto(True, icon)
window.config(background='#696868')
window.geometry("670x600")
window.resizable(False, False)
player1_cards_total = 0
player2_cards_total = 0
main_label = Label(window, text='Player 1 Turn!', font=('Ariel', 40, 'bold'), fg='Black', bg="#696868")
player1_card_count_label = Label(window, text="Card Count: 0", font=('Ariel', 30, 'bold'), pady=30, fg='black', bg='#696868')
player2_card_count_label = Label(window, text="Card Count: 0", font=('Ariel', 30, 'bold'), pady=30, fg='black', bg='#696868')
def welcome_screen():
global welcome_label
global instruction_label
global rules_label
global move_to_instructions_button
global move_to_game_button
global move_to_welcome_button
label_remover()
welcome_label.grid(row=0, column=0, columnspan=2, sticky="e")
move_to_instructions_button.grid(row=1, column=0, padx=20)
move_to_game_button.grid(row=1, column=1)
def instructions_maker():
global welcome_label
global instruction_label
global rules_label
global move_to_instructions_button
global move_to_game_button
global move_to_welcome_button
label_remover()
instruction_label.grid(row=0, column=0, columnspan=2)
rules_label.grid(row=1, column=0, columnspan=2)
move_to_welcome_button.grid(row=2, column=0)
move_to_game_button.grid(row=2, column=1)
def main():
global instruction_label
global rules_label
global move_to_game_button
global move_to_welcome_button
global move_to_instructions_button
label_remover()
move_to_welcome_button.grid(row=4, column=0)
move_to_instructions_button.grid(row=4, column=1)
def player1_draw_card():
global player1_cards_total
player1_cards_total += random.randint(1, 13)
player1_card_count_label.config(text="Card Count: " + str(player1_cards_total))
if player1_cards_total == 21:
main_label.config(text="Player 2 Turn!")
player1_draw_card_button.config(state=DISABLED)
player1_999.config(state=DISABLED)
player2_draw_card_button.config(state=NORMAL)
player2_999.config(state=NORMAL)
if player1_cards_total > 21:
main_label.config(text="Player 2 Won!")
stop_game()
def player1_stop_play(p1_entry_input):
if player1_999.get()[len(player1_999.get()) - 1] != '9':
x = player1_999.get()
player1_999.delete(0, END)
player1_999.insert(0, x[0:len(x) - 1])
elif player1_999.get() == '999':
player1_draw_card_button.config(state=DISABLED)
player1_999.config(state=DISABLED)
player2_draw_card_button.config(state=NORMAL)
player2_999.config(state=NORMAL)
main_label.config(text="Player 2 Turn!")
def player2_draw_card():
global player1_cards_total
global player2_cards_total
player2_cards_total += random.randint(1, 13)
player2_card_count_label.config(text="Card Count: " + str(player2_cards_total))
if player1_cards_total < player2_cards_total <= 21:
main_label.config(text="Player 2 Won!")
player1_draw_card_button.config(state=DISABLED)
player1_999.config(state=DISABLED)
player2_draw_card_button.config(state=DISABLED)
player2_999.config(state=DISABLED)
stop_game()
elif player1_cards_total == player2_cards_total == 21:
main_label.config(text="Draw! Lets Reset!")
player2_draw_card_button.config(state=DISABLED)
player2_999.config(state=DISABLED)
window.after(2500, reset_game)
elif player2_cards_total > 21:
main_label.config(text="Player 1 Won!")
stop_game()
def player2_stop_play(p2_entry_input):
if player2_999.get()[len(player2_999.get()) - 1] != '9':
x = player2_999.get()
player2_999.delete(0, END)
player2_999.insert(0, x[0:len(x) - 1])
elif player2_999.get() == '999':
global player1_cards_total
global player2_cards_total
if player1_cards_total < player2_cards_total <= 21:
main_label.config(text="Player 2 Won!")
player1_draw_card_button.config(state=DISABLED)
player1_999.config(state=DISABLED)
player2_draw_card_button.config(state=DISABLED)
player2_999.config(state=DISABLED)
stop_game()
restart_button.grid(row=5, column=0, columnspan=2, pady=10)
elif player1_cards_total == player2_cards_total and player2_cards_total <= 21:
main_label.config(text="Draw! Lets Reset!")
player2_draw_card_button.config(state=DISABLED)
player2_999.config(state=DISABLED)
window.after(3500, reset_game)
elif player2_cards_total < player1_cards_total <= 21:
main_label.config(text="Player 1 Won!")
stop_game()
def reset_game():
global player1_cards_total
global player2_cards_total
main_label.config(text="Player 1 Turn")
player1_cards_total = 0
player2_cards_total = 0
player1_card_count_label.config(text="Card Count: " + str(player1_cards_total))
player2_card_count_label.config(text="Card Count: " + str(player2_cards_total))
player1_draw_card_button.config(state=NORMAL)
player1_999.config(state=NORMAL)
player2_draw_card_button.config(state=DISABLED)
player2_999.config(state=NORMAL)
player1_999.delete(0, END)
player2_999.delete(0, END)
player2_999.config(state=DISABLED)
try:
restart_button.grid_remove()
except:
pass
def stop_game():
global player1_cards_total
global player2_cards_total
global restart_button
player1_draw_card_button.config(state=DISABLED)
player1_999.config(state=DISABLED)
player2_draw_card_button.config(state=DISABLED)
player2_999.config(state=DISABLED)
restart_button.grid(row=5, column=0, columnspan=2, pady=10)
player1_draw_card_button = Button(text="player 1" + "\n" + "draw card", padx=80, pady=50, bg="#212121", fg="#FFFFFF",
command=player1_draw_card, font=('Ariel', 20, 'bold'), state=NORMAL)
player2_draw_card_button = Button(text="player 2" + "\n" + "draw card", padx=80, pady=50, bg="#212121", fg="#FFFFFF",
command=player2_draw_card, font=('Ariel', 20, 'bold'), state=DISABLED)
player1_999 = Entry(window, state=NORMAL, font=('Ariel', 30, 'bold'), width=5, fg='black', bg='#3b3b3b',
disabledbackground='#212121', disabledforeground='black')
player2_999 = Entry(window, state=DISABLED, font=('Ariel', 30, 'bold'), width=5, fg='black', bg='#3b3b3b',
disabledbackground='#212121', disabledforeground='black')
player1_999.bind("<KeyRelease>", player1_stop_play)
player2_999.bind("<KeyRelease>", player2_stop_play)
main_label.grid(row=0, column=0, columnspan=2)
player1_card_count_label.grid(row=1, column=0)
player1_draw_card_button.grid(row=2, column=0, padx=10)
player1_999.grid(row=3, column=0, pady=20, ipadx=30)
player2_card_count_label.grid(row=1, column=1)
player2_draw_card_button.grid(row=2, column=1, padx=10)
player2_999.grid(row=3, column=1, pady=20, ipadx=30)
def label_remover():
welcome_label.grid_remove()
instruction_label.grid_remove()
rules_label.grid_remove()
move_to_instructions_button.grid_remove()
move_to_game_button.grid_remove()
move_to_welcome_button.grid_remove()
main_label.grid_remove()
player1_card_count_label.grid_remove()
player2_card_count_label.grid_remove()
welcome_label = Label(window, text=" welcome to my Black Jack game!" + "\n" + " made by ziv lahav",
font=('Ariel', 20, 'bold'), pady=30, fg='black', bg='#696868')
move_to_instructions_button = Button(text='instructions screen', width=20, height=5, bg="#212121", fg="#FFFFFF",
command=instructions_maker, font=('Ariel', 12, 'bold'), state=NORMAL)
move_to_game_button = Button(text='start game', width=20, height=5, bg="#212121", fg="#FFFFFF", command=main,
font=('Ariel', 12, 'bold'), state=NORMAL)
instruction_label = Label(window, text=" instructions:\n the player draws cards valued at 1-13. \n"
" when the player wants to switch turns,\n he writes '999' in the input box.",
font=('Ariel', 20, 'bold'), pady=30, fg='black', bg='#696868')
rules_label = Label(window, text=" goal:\n the goal is to get as close as you get to 21.\n"
" if a player's total card count > 21, he looses", font=('Ariel', 20, 'bold'), pady=30,
fg='black', bg='#696868')
move_to_welcome_button = Button(text="welcome screen", width=20, height=5, bg="#212121", fg="#FFFFFF", command=welcome_screen,
font=('Ariel', 12, 'bold'), state=NORMAL)
restart_button = Button(text="Click Here To Restart", width=20, height=5, bg="#212121", fg="#FFFFFF", command=reset_game,
font=('Ariel', 9, 'bold'), state=NORMAL)
welcome_screen()
window.mainloop()
restart_button can't see inside main(). You have a bunch of functions defined within the main() function for some reason, including reset_game, and so anything that's not in the scope of main() can't see those functions. The solution is to move the functions outside of main().
#RandomDavis gave a good answer, what I can suggest is, you can work in a class. Make a main class and than add other functions inside it. It may work that way!
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.
*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 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)