Related
I cannot get this text box to work with CTkinter
ERROR
Traceback (most recent call last):
File "C:\Users\James Martin\PycharmProjects\Cal\Cal.py", line 43, in
textbox = customtkinter.CTkTextbox(app, width=18, height=2, CTkFont=("Roboto", 24))
File "C:\Users\James Martin\PycharmProjects\Cal\Lib\site-packages\customtkinter\windows\widgets\ctk_textbox.py", line 91, in init
check_kwargs_empty(kwargs, raise_error=True)
File "C:\Users\James Martin\PycharmProjects\Cal\Lib\site-packages\customtkinter\windows\widgets\utility\utility_functions.py", line 18, in check_kwargs_empty
raise ValueError(f"{list(kwargs_dict.keys())} are not supported arguments. Look at the documentation for supported arguments.")
ValueError: ['CTkFont'] are not supported arguments. Look at the documentation for supported arguments.
This is the whole code
import tkinter
import customtkinter
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("blue")
class App(customtkinter.CTk):
cal_prb = ""
def __int__(self):
super().__init__()
self.title("Cal")
self.geometry("265x225")
def add_to_prb(symbol):
global cal_prb
cal_prb += str(symbol)
textbox.delete(1.0, "end")
textbox.insert(1.0, cal_prb)
def slv_prb():
global cal_prb
try:
cal_prb = str(eval(cal_prb))
textbox.delete(1.0, "end")
textbox.insert(1.0, cal_prb)
except:
clr_prb()
textbox.insert(1.0, "Error")
pass
App = customtkinter.CTkFrame
def clr_prb():
global cal_prb
cal_prb = ""
textbox.delete(1.0, "end")
textbox = customtkinter.CTkTextbox(App, width=18, height=2, CTkFont=("Roboto", 24))
textbox.grid(columnspan=5)
textbox.configure(state="disabled")
btn_1 = customtkinter.CTkButton(App, text="1", command=lambda: add_to_prb(1), width=6, CTkFont=("Roboto", 14))
btn_1.grid(row=2, column=1)
btn_2 = customtkinter.CTkButton(App, text="2", command=lambda: add_to_prb(2), width=6, CTkFont="Roboto")
btn_2.grid(row=2, column=2)
btn_3 = customtkinter.CTkButton(App, text="3", command=lambda: add_to_prb(3), width=6, CTkFont="Roboto")
btn_3.grid(row=2, column=3)
btn_4 = customtkinter.CTkButton(App, text="4", command=lambda: add_to_prb(4), width=6, CTkFont="Roboto")
btn_4.grid(row=3, column=1)
btn_5 = customtkinter.CTkButton(App, text="5", command=lambda: add_to_prb(5), width=6, CTkFont="Roboto")
btn_5.grid(row=3, column=2)
btn_6 = customtkinter.CTkButton(App, text="6", command=lambda: add_to_prb(6), width=6, CTkFont="Roboto")
btn_6.grid(row=3, column=3)
btn_7 = customtkinter.CTkButton(App, text="7", command=lambda: add_to_prb(7), width=6, CTkFont="Roboto")
btn_7.grid(row=4, column=1)
btn_8 = customtkinter.CTkButton(App, text="8", command=lambda: add_to_prb(8), width=6, CTkFont="Roboto")
btn_8.grid(row=4, column=2)
btn_9 = customtkinter.CTkButton(App, text="9", command=lambda: add_to_prb(9), width=6, CTkFont="Roboto")
btn_9.grid(row=4, column=3)
btn_0 = customtkinter.CTkButton(App, text="0", command=lambda: add_to_prb(0), width=6, CTkFont="Roboto")
btn_0.grid(row=5, column=1)
btn_percent = customtkinter.CTkButton(App, text="%", command=lambda: add_to_prb("%"), width=5, CTkFont="Roboto")
btn_percent.grid(row=1, column=4)
btn_plus = customtkinter.CTkButton(App, text="+", command=lambda: add_to_prb("+"), width=5, CTkFont="Roboto")
btn_plus.grid(row=2, column=4)
btn_minus = customtkinter.CTkButton(App, text="-", command=lambda: add_to_prb("-"), width=5, CTkFont="Roboto")
btn_minus.grid(row=3, column=4)
btn_times = customtkinter.CTkButton(App, text="X", command=lambda: add_to_prb("*"), width=5, CTkFont="Roboto")
btn_times.grid(row=4, column=4)
btn_div = customtkinter.CTkButton(App, text="/", command=lambda: add_to_prb("/"), width=5, CTkFont="Roboto")
btn_div.grid(row=5, column=4)
btn_open = customtkinter.CTkButton(App, text="(", command=lambda: add_to_prb("("), width=6, CTkFont="Roboto")
btn_open.grid(row=1, column=1)
btn_close = customtkinter.CTkButton(App, text=")", command=lambda: add_to_prb(")"), width=6, CTkFont="Roboto")
btn_close.grid(row=1, column=2)
btn_dot = customtkinter.CTkButton(App, text=".", command=lambda: add_to_prb("."), width=6, CTkFont="Roboto")
btn_dot.grid(row=1, column=3)
btn_slv = customtkinter.CTkButton(App, text="=", command=lambda: slv_prb(), width=6, CTkFont="Roboto")
btn_slv.grid(row=5, column=2)
btn_clr = customtkinter.CTkButton(App, text="CLR", command=lambda: clr_prb(), width=6, CTkFont="Roboto")
btn_clr.grid(row=5, column=3)
App.mainloop()
Does this help?
Change this:
App = customtkinter.CTkFrame
to:
app = App() #Don't use capital A in variable.
Put this:
app = App()
app.mainloop()
I'm currently working on a hangman game in python using tkinter. My code is working but I would like to make it better. My question is how do I change the existing word list into a input from another player? I think I have to create a play button for the actual game to start how?
Thanks for your time!
from tkinter import messagebox
from tkinter import *
import random
#Setting up the window
window = Tk()
window.title("Välkommen till Rädda Gubbe")
window.geometry("350x250+525+200")
lblspel = Label(window, text="Rädda Gubbe", font=('Arial', '16', 'bold', 'italic'), justify='center', fg='Red'
).pack(side=TOP, anchor=CENTER,pady=7)
Namnlbl = Label(window, text="Skriv in det hemliga ordet: ", font=('Arial', '11', 'bold', 'italic'), justify='center'
).pack(side=TOP,pady=2)
e = Entry(window, font=('Arial', 12), justify='center')
e.pack(side=TOP)
startaspelet = Button(window, text="Starta Spelet", relief="solid", font=("arial", 12, "bold"), command=lambda: spelet).place(x=30, y=70)
def spelet():
window2 = Tk()
window2.title("Rädda Gubbe")
window2.resizable(0, 0)
#Words
word_list = ['FLAMBOYANT','AMBIGUOUS','AMBIVALENT','CHARISMA']
def newGame():
global the_word_withSpaces
global numberOfGuesses
numberOfGuesses=0
the_word=random.choice(word_list)
the_word_withSpaces=" ".join(the_word)
lblWord.set(" ".join("_"*len(the_word)))
def guess(letter):
global numberOfGuesses
if numberOfGuesses<11:
txt=list(the_word_withSpaces)
guessed=list(lblWord.get())
if the_word_withSpaces.count(letter)>0:
for c in range(len(txt)):
if txt[c]==letter:
guessed[c]=letter
lblWord.set("".join(guessed))
if lblWord.get()==the_word_withSpaces:
messagebox.showinfo("Hangman","You guessed it!")
newGame()
else:
numberOfGuesses+=1
if numberOfGuesses==11:
messagebox.showwarning("Hangman","Game over")
lblWord=StringVar()
Label(window, textvariable=lblWord, font=("Consolas 24 bold")).grid(row=0, column=3, columnspan=6, padx=10)
btn1 = Button(window2, text="A",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("A"))
btn1.grid(column=1, row=1)
btn2 = Button(window2, text="B",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("B"))
btn2.grid(column=2, row=1)
btn3 = Button(window2, text="C",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("C"))
btn3.grid(column=3, row=1)
btn4 = Button(window2, text="D",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("D"))
btn4.grid(column=4, row=1)
btn5 = Button(window2, text="E",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("E"))
btn5.grid(column=5, row=1)
btn6 = Button(window2, text="F",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("F"))
btn6.grid(column=6, row=1)
btn7 = Button(window2, text="G",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("G"))
btn7.grid(column=7, row=1)
btn9 = Button(window2, text="H",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("H"))
btn9.grid(column=1, row=2)
btn10 = Button(window2, text="I",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("I"))
btn10.grid(column=2, row=2)
btn11 = Button(window2, text="J",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("J"))
btn11.grid(column=3, row=2)
btn12 = Button(window2, text="K",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("K"))
btn12.grid(column=4, row=2)
btn13 = Button(window2, text="L",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("L"))
btn13.grid(column=5, row=2)
btn14 = Button(window2, text="M",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("M"))
btn14.grid(column=6, row=2)
btn15 = Button(window2, text="N",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("N"))
btn15.grid(column=7, row=2)
btn16 = Button(window2, text="O",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("O"))
btn16.grid(column=1, row=3)
btn17 = Button(window2, text="P",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("P"))
btn17.grid(column=2, row=3)
btn18 = Button(window2, text="Q",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("Q"))
btn18.grid(column=3, row=3)
btn19 = Button(window2, text="R",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("R"))
btn19.grid(column=4, row=3)
btn20 = Button(window2, text="S",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("S"))
btn20.grid(column=5, row=3)
btn21 = Button(window2, text="T",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("T"))
btn21.grid(column=6, row=3)
btn22 = Button(window2, text="U",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("U"))
btn22.grid(column=7, row=3)
btn23 = Button(window2, text="V",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("V"))
btn23.grid(column=1, row=4)
btn24 = Button(window2, text="W",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("W"))
btn24.grid(column=2, row=4)
btn25 = Button(window2, text="X",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("X"))
btn25.grid(column=3, row=4)
btn26 = Button(window2, text="Y",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("Z"))
btn26.grid(column=4, row=4)
btn27 = Button(window2, text="Å",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("Å"))
btn27.grid(column=5, row=4)
btn28 = Button(window2, text="Ä",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("Ä"))
btn28.grid(column=6, row=4)
btn29 = Button(window2, text="Ö",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'), command= lambda : guess("Ö"))
btn29.grid(column=7, row=4)
Button(window, text="New\nGame", command=lambda:newGame(), font=("Helvetica 10 bold")).grid(row=3, column=8, sticky="NSWE")
newGame()
window.mainloop()
The easiest way is to create file words.txt where second player can put his words:
File words.txt:
Strength
Perception
Endurance
Charisma
Intelligence
Agility
Luck
Code:
from tkinter import messagebox
from tkinter import *
import random
from string import ascii_uppercase
# Setting up the window
window = Tk()
window.title("Hangman")
window.resizable(0, 0)
def newGame():
global the_word_withSpaces
global numberOfGuesses
numberOfGuesses = 0
with open('words.txt', 'r', encoding='utf-8') as f:
words = [w.strip().upper() for w in f.readlines()]
the_word = random.choice(words)
the_word_withSpaces = " ".join(the_word)
lblWord.set(" ".join("_" * len(the_word)))
def guess(letter):
global numberOfGuesses
if numberOfGuesses < 11:
txt = list(the_word_withSpaces)
guessed = list(lblWord.get())
if the_word_withSpaces.count(letter) > 0:
for c in range(len(txt)):
if txt[c] == letter:
guessed[c] = letter
lblWord.set("".join(guessed))
if lblWord.get() == the_word_withSpaces:
messagebox.showinfo("Hangman", "You guessed it!")
newGame()
else:
numberOfGuesses += 1
if numberOfGuesses == 11:
messagebox.showwarning("Hangman", "Game over")
lblWord = StringVar()
Label(window, textvariable=lblWord, font=("Consolas 24 bold")).grid(row=0, column=3, columnspan=6, padx=10)
btn1 = Button(window, text="A", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("A"))
btn1.grid(column=1, row=1)
btn2 = Button(window, text="B", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("B"))
btn2.grid(column=2, row=1)
btn3 = Button(window, text="C", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("C"))
btn3.grid(column=3, row=1)
btn4 = Button(window, text="D", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("D"))
btn4.grid(column=4, row=1)
btn5 = Button(window, text="E", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("E"))
btn5.grid(column=5, row=1)
btn6 = Button(window, text="F", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("F"))
btn6.grid(column=6, row=1)
btn7 = Button(window, text="G", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("G"))
btn7.grid(column=7, row=1)
btn9 = Button(window, text="H", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("H"))
btn9.grid(column=1, row=2)
btn10 = Button(window, text="I", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("I"))
btn10.grid(column=2, row=2)
btn11 = Button(window, text="J", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("J"))
btn11.grid(column=3, row=2)
btn12 = Button(window, text="K", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("K"))
btn12.grid(column=4, row=2)
btn13 = Button(window, text="L", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("L"))
btn13.grid(column=5, row=2)
btn14 = Button(window, text="M", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("M"))
btn14.grid(column=6, row=2)
btn15 = Button(window, text="N", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("N"))
btn15.grid(column=7, row=2)
btn16 = Button(window, text="O", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("O"))
btn16.grid(column=1, row=3)
btn17 = Button(window, text="P", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("P"))
btn17.grid(column=2, row=3)
btn18 = Button(window, text="Q", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("Q"))
btn18.grid(column=3, row=3)
btn19 = Button(window, text="R", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("R"))
btn19.grid(column=4, row=3)
btn20 = Button(window, text="S", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("S"))
btn20.grid(column=5, row=3)
btn21 = Button(window, text="T", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("T"))
btn21.grid(column=6, row=3)
btn22 = Button(window, text="U", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("U"))
btn22.grid(column=7, row=3)
btn23 = Button(window, text="V", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("V"))
btn23.grid(column=1, row=4)
btn24 = Button(window, text="W", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("W"))
btn24.grid(column=2, row=4)
btn25 = Button(window, text="X", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("X"))
btn25.grid(column=3, row=4)
btn26 = Button(window, text="Y", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("Z"))
btn26.grid(column=4, row=4)
btn27 = Button(window, text="Å", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("Å"))
btn27.grid(column=5, row=4)
btn28 = Button(window, text="Ä", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("Ä"))
btn28.grid(column=6, row=4)
btn29 = Button(window, text="Ö", bg="skyBlue", fg="Black", width=3, height=1, font=('Helvetica', '20'),
command=lambda: guess("Ö"))
btn29.grid(column=7, row=4)
Button(window, text="New\nGame", command=lambda: newGame(), font=("Helvetica 10 bold")).grid(row=3, column=8,
sticky="NSWE")
newGame()
window.mainloop()
Output:
I have coded the basic idea. Add 2 frames one a game_frame and the other home_frame. Now just use .pack and .pack_forget to switch between frames
Here is the sample code (modify this to suit your need)
from tkinter import messagebox
from tkinter import *
from tkinter import ttk
import random
def assignWord(word):
global guess_word, the_word_withSpaces
guess_word = word.upper()
home_frame.pack_forget()
game_frame.pack()
print(guess_word)
guess_var.set(" ".join("_"*len(guess_word)))
the_word_withSpaces = " ".join(word.upper())
def createWord():
global currentPlayer
currentPlayer = playerChoose.get()
top = Toplevel(window)
ent = Entry(top, show='*')
ent.pack()
Button(top, text='Submit', command= lambda : [assignWord(ent.get()), top.destroy()]).pack()
def newGame():
global numberOfGuesses
numberOfGuesses = 0
game_frame.pack_forget()
home_frame.pack()
def guess(btn):
global numberOfGuesses
letter = btn['text']
if numberOfGuesses<11:
txt=list(the_word_withSpaces)
guessed=list(guess_var.get())
if the_word_withSpaces.count(letter)>0:
for c in range(len(txt)):
if txt[c]==letter:
guessed[c]=letter
guess_var.set("".join(guessed))
if guess_var.get()==the_word_withSpaces:
messagebox.showinfo("Hangman",f"{currentPlayer} guessed it!")
newGame()
return
else:
numberOfGuesses+=1
if numberOfGuesses==11:
messagebox.showwarning("Hangman","Game over")
newGame()
window = Tk()
window.title("Välkommen till Rädda Gubbe")
window.geometry("350x250+525+200")
numberOfGuesses = 0
players = ['Player1', 'Player2']
guess_word = "HELLO"
the_word_withSpaces = ''
currentPlayer = players[0]
alpha = [chr(x) for x in range(65,89)]
home_frame = Frame(window)
home_frame.pack()
Label(home_frame, text="Choose the player to enter a word: ").pack()
playerChoose = ttk.Combobox(home_frame, values=players, state='readonly')
playerChoose.set(players[0])
playerChoose.pack()
Button(home_frame, text='Enter word', command=createWord).pack()
game_frame = Frame(window)
guess_var = StringVar()
Label(game_frame, textvariable = guess_var).pack(side=TOP, anchor=CENTER,pady=7)
btn_frame = Frame(game_frame)
btn_frame.pack()
row, column = 0, 0
for x in alpha:
if column%7 == 0:
row += 1
column = 0
btn = Button(btn_frame, text=x)
btn.config(command = lambda b=btn: guess(b))
btn.grid(row=row, column=column)
column += 1
window.mainloop()
I'm trying to create a roulette table (which I have done as a GUI). Now, when I press the button for the outcome it shows the number in my top box, but I'm struggling to find a way to collect the results in a list, so I can then later pull data from the list.
For example, I want a list of past results so that I could show that's it's been 4 spins without landing on a odd number or it's been 3 spins since it's been red, or on average it lands in the 1st 12 every 3 spins.
I have tried numerous ways of collecting results from the buttons clicked but nothing works.
from tkinter import*
# Roulette GUI
root = Tk()
root.title("Roulette")
e = Entry(root, width=200, borderwidth=5)
e.grid(row=0, column=0, columnspan=13, padx=10, pady=10)
def button_click(number):
e.delete(0, END)
e.insert(0, number)
print(number)
# define buttons
button_0 = Button(root, text="0", padx=40, pady=84, bg='green', command=lambda: button_click(0))
button_1 = Button(root, text="1", padx=40, pady=20, bg='red', command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, bg='red', command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, bg='red', command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, bg='red', command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, bg='red', command=lambda: button_click(9))
button_10 = Button(root, text="10", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(10))
button_11 = Button(root, text="11", padx=40, pady=20, bg='red', command=lambda: button_click(11))
button_12 = Button(root, text="12", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(12))
button_13 = Button(root, text="13", padx=40, pady=20, bg='red', command=lambda: button_click(13))
button_14 = Button(root, text="14", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(14))
button_15 = Button(root, text="15", padx=40, pady=20, bg='red', command=lambda: button_click(15))
button_16 = Button(root, text="16", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(16))
button_17 = Button(root, text="17", padx=40, pady=20, bg='red', command=lambda: button_click(17))
button_18 = Button(root, text="18", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(18))
button_19 = Button(root, text="19", padx=40, pady=20, bg='red', command=lambda: button_click(19))
button_20 = Button(root, text="20", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(20))
button_21 = Button(root, text="21", padx=40, pady=20, bg='red', command=lambda: button_click(21))
button_22 = Button(root, text="22", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(22))
button_23 = Button(root, text="23", padx=40, pady=20, bg='red', command=lambda: button_click(23))
button_24 = Button(root, text="24", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(24))
button_25 = Button(root, text="25", padx=40, pady=20, bg='red', command=lambda: button_click(25))
button_26 = Button(root, text="26", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(26))
button_27 = Button(root, text="27", padx=40, pady=20, bg='red', command=lambda: button_click(27))
button_28 = Button(root, text="28", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(28))
button_29 = Button(root, text="29", padx=40, pady=20, bg='red', command=lambda: button_click(29))
button_30 = Button(root, text="30", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(30))
button_31 = Button(root, text="31", padx=40, pady=20, bg='red', command=lambda: button_click(31))
button_32 = Button(root, text="32", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(32))
button_33 = Button(root, text="33", padx=40, pady=20, bg='red', command=lambda: button_click(33))
button_34 = Button(root, text="34", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(34))
button_35 = Button(root, text="35", padx=40, pady=20, bg='red', command=lambda: button_click(35))
button_36 = Button(root, text="36", padx=40, pady=20, bg='black', fg='white', command=lambda: button_click(36))
button_exit = Button(root, text="Exit", padx=60, pady=20, command=root.quit)
# put buttons on the screen
button_0.grid(row=1, rowspan=3, column=0)
button_1.grid(row=3, column=1)
button_2.grid(row=2, column=1)
button_3.grid(row=1, column=1)
button_4.grid(row=3, column=2)
button_5.grid(row=2, column=2)
button_6.grid(row=1, column=2)
button_7.grid(row=3, column=3)
button_8.grid(row=2, column=3)
button_9.grid(row=1, column=3)
button_10.grid(row=3, column=4)
button_11.grid(row=2, column=4)
button_12.grid(row=1, column=4)
button_13.grid(row=3, column=5)
button_14.grid(row=2, column=5)
button_15.grid(row=1, column=5)
button_16.grid(row=3, column=6)
button_17.grid(row=2, column=6)
button_18.grid(row=1, column=6)
button_19.grid(row=3, column=7)
button_20.grid(row=2, column=7)
button_21.grid(row=1, column=7)
button_22.grid(row=3, column=8)
button_23.grid(row=2, column=8)
button_24.grid(row=1, column=8)
button_25.grid(row=3, column=9)
button_26.grid(row=2, column=9)
button_27.grid(row=1, column=9)
button_28.grid(row=3, column=10)
button_29.grid(row=2, column=10)
button_30.grid(row=1, column=10)
button_31.grid(row=3, column=11)
button_32.grid(row=2, column=11)
button_33.grid(row=1, column=11)
button_34.grid(row=3, column=12)
button_35.grid(row=2, column=12)
button_36.grid(row=1, column=12)
button_exit.grid(row=4, column=11, columnspan=2)
root.mainloop()
if you are trying to see percentage of pressing red and black buttons, this code print it on your console
from tkinter import*
root = Tk()
root.title("Roulette")
e = Entry(root, width=200, borderwidth=5)
e.grid(row=0, column=0, columnspan=13, padx=10, pady=10)
results = []
def button_click(number):
e.delete(0, END)
e.insert(0, number)
results.append(number)
#loop through results
count = 0
for i in range(len(results)):
if results[i]%2 == 0:
count += 1
print("Number is",number)
red_percentage = count/len(results)*100
print("Black", 100-red_percentage)
print("Red", red_percentage)
print("------------------------------------")
# define buttons
button = Button(root, text="0", padx=40, pady=84, bg='green', command=lambda: button_click(0))
button.grid(row=1, rowspan=3, column=0)
for i in range(0,36):
if i%2 == 0:
button = Button(root, text=str(i+1), padx=40, pady=20, bg='black', fg='white', command=lambda i=i:button_click(i+1))
else:
button = Button(root, text=str(i+1), padx=40, pady=20, bg='red', command=lambda i=i: button_click(i+1))
button.grid(row=3 - i%3, column=int(i/3) + 1)
button = Button(root, text="Exit", padx=60, pady=20, command=root.quit)
button.grid(row=4, column=11, columnspan=2)
root.mainloop()
I have two files in my directory (first.py and second.py). The first.py has a button. So on clicking the button in first.py gui window, it should be directed to second.py gui window.
first.py window photo and second.py window photo. So on clicking the sign up button in the first.py, it should go to the sign up page in second.py.
How to do the connection or linking between the two scripts?
first.py
import tkinter as tk
root=tk.Tk()
root.title("My Bank")
root.geometry("500x500")
photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)
tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)
w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")
bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)
w = tk.Label(root, text="Main Menu", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)
button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=10, pady=10)
button2 = tk.Button(root, text="Sign In", width=12, height=1,
bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button2.pack(padx=10, pady=10)
button3 = tk.Button(root, text="Admin Sign In", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button3.pack(padx=10, pady=10)
button4 = tk.Button(root, text="Quit!", width=5, height=1, bg="black",fg="white", bd="10", font=("Helvetica", "12", "bold"))
button4.pack(padx=10, pady=10)
root.mainloop()
second.py
import tkinter as tk
root=tk.Tk()
root.title("My Bank")
root.geometry("500x500")
photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)
tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)
w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")
bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)
w = tk.Label(root, text="Sign Up", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)
e1 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e1.insert(0, 'Username')
e1.pack(padx=150, pady=10)
e2 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e2.insert(0, 'Email')
e2.pack(padx=150, pady=10)
e3 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e3.insert(0, 'Password')
e3.pack(padx=150, pady=10)
button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=100, pady=20)
root.mainloop()
I went through your problem and I guess you want to open another file when the button is clicked. To do this you need to import the file which you want to load on button click. And create another tkinter function in the external script. While running through your code i even came across the error which is tkinter.TclError: image "pyimage3" doesn't exist as of now I even fixed this for more information visit this link. Here is the code which i made all the changes.
"""
Spyder Editor
This is a temporary script file.
"""
import second
import tkinter as tk
root=tk.Toplevel()
root.title("My Bank")
root.geometry("500x500")
photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)
tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)
w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")
bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)
w = tk.Label(root, text="Main Menu", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)
button1 = tk.Button(root, text="Sign Up", command=lambda : second.signup() , width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=10, pady=10)
button2 = tk.Button(root, text="Sign In", width=12, height=1,
bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button2.pack(padx=10, pady=10)
button3 = tk.Button(root, text="Admin Sign In", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button3.pack(padx=10, pady=10)
button4 = tk.Button(root, text="Quit!", width=5, height=1, bg="black",fg="white", bd="10", font=("Helvetica", "12", "bold"))
button4.pack(padx=10, pady=10)
root.mainloop()
and the other one
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 7 11:09:28 2018
#author: kedar
"""
import tkinter as tk
def signup():
root=tk.Toplevel()
root.title("My Bank")
root.geometry("500x500")
photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)
tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)
w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")
bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)
w = tk.Label(root, text="Sign Up", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)
e1 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e1.insert(0, 'Username')
e1.pack(padx=150, pady=10)
e2 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e2.insert(0, 'Email')
e2.pack(padx=150, pady=10)
e3 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e3.insert(0, 'Password')
e3.pack(padx=150, pady=10)
button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=100, pady=20)
root.mainloop()
For now you can just copy this and use it. I hope it helps.
My code for my messagebox won't work on my window. Everything else works but when I run it my "About" messagebox won't show up. I want my messagebox to pop out when "About" is clicked on my window. What can I do to make it work?
from tkinter import *
from tkinter import messagebox
calculator = Tk()
calculator.title("Calculator")
calculator.geometry("317x145")
menubar = Menu(calculator)
class Calculator(Frame):
def __init__(self):
Frame.__init__(self)
display = Frame(calculator, bd=0, width=1000, height=1000, relief=SUNKEN)
buttons = Frame(calculator, bd=0, width=7, height=1, relief=GROOVE)
display.grid(column=0, row=0, padx=0, pady=0)
buttons.grid(column=0, row=1, padx=1)
numbers = StringVar()
self.results = Entry(display, textvariable=numbers, width=31, fg="DarkOrchid4", bg="lavender blush", font="Verdana")
self.results.pack()
self.results.grid(column=0, row=0)
def showup(x):
return lambda: self.results.insert(END, x)
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for i in range(9):
n=numbers[i]
Button(buttons, bg="snow", text=n, width=7, height=1, command=showup(n), relief=RAISED).grid(row=i%3, column=i//3)
Clear = Button(buttons, bg="snow", text="C", width=7, height=1, command=self.clear, relief=RAISED)
Clear.grid(padx=2, pady=2, column=3, row=0)
Equals = Button(buttons, bg="snow", text="=", width=7, height=1, command=self.equals, relief=RAISED)
Equals.grid(padx=2, pady=2, column=4, row=3)
All_clear = Button(buttons, bg="snow", text="AC", width=7, height=1, command=self.all_clear, relief=RAISED)
All_clear.grid(padx=2, pady=2, column=4, row=0)
Bracket_one = Button(buttons, bg="snow", text="(", width=7, height=1, command=self.bracket_one, relief=RAISED)
Bracket_one.grid(padx=2, pady=2, column=2, row=3)
Bracket_two = Button(buttons, bg="snow", text=")", width=7, height=1, command=self.bracket_two, relief=RAISED)
Bracket_two.grid(padx=2, pady=2, column=3, row=3)
Zero = Button(buttons, bg="snow", text="0", width=7, height=1, command=self.zero, relief=RAISED)
Zero.grid(padx=2, pady=2, column=0, row=3)
Decimal_point = Button(buttons, bg="snow", text=".", width=7, height=1, command=self.decimal_point, relief=RAISED)
Decimal_point.grid(padx=2, pady=2, column=1, row=3)
Multiplication = Button(buttons, bg="red", text="x", width=7, height=1, command=self.multiplication, relief=RAISED)
Multiplication.grid(padx=2, pady=2, column=3, row=1)
Division = Button(buttons, bg="powder blue", text="/", width=7, height=1, command=self.division, relief=RAISED)
Division.grid(padx=2, pady=2, column=4, row=1)
Addition = Button(buttons, bg="yellow", text="+", width=7, height=1, command=self.addition, relief=RAISED)
Addition.grid(padx=2, pady=2, column=3, row=2)
Subtraction = Button(buttons, bg="green", text="-", width=7, height=1, command=self.subtraction, relief=RAISED)
Subtraction.grid(padx=2, pady=2, column=4, row=2)
def equals(self):
try:
result = eval(self.results.get())
except:
result = "Invalid input"
self.all_clear()
self.results.insert(0, result)
def zero(self):
self.results.insert(END, "0")
def bracket_one(self):
self.results.insert(END, "(")
def bracket_two(self):
self.results.insert(END, ")")
def all_clear(self):
self.results.delete(0, END)
def clear(self):
self.results.delete(-1)
def multiplication(self):
self.results.insert(END, "*")
def division(self):
self.results.insert(END, "/")
def addition(self):
self.results.insert(END, "+")
def subtraction(self):
self.results.insert(END, "-")
def decimal_point(self):
self.results.insert(END, ".")
def about():
messagebox.showinfo(title = "About", message = "Author")
return
helpMenu = Menu(menubar)
menubar.add_command(label = "About", command=about)
if __name__ == '__main__':
Calculator().mainloop()
calculator.config(menu=menubar)
calculator.mainloop()
Your issue is that you are not associating the menubar with your root application. That is why it is never coming up. You need to configure the menu for your application to be the menubar you create. Example -
calculator.configure(menu=menubar)
Also, it would be better to move that code inside you frame as well. Example -
from tkinter import *
from tkinter import messagebox
calculator = Tk()
calculator.title("Calculator")
calculator.geometry("317x145")
menubar = Menu(calculator)
class Calculator(Frame):
def __init__(self):
Frame.__init__(self)
display = Frame(calculator, bd=0, width=1000, height=1000, relief=SUNKEN)
buttons = Frame(calculator, bd=0, width=7, height=1, relief=GROOVE)
display.grid(column=0, row=0, padx=0, pady=0)
buttons.grid(column=0, row=1, padx=1)
numbers = StringVar()
self.results = Entry(display, textvariable=numbers, width=31, fg="DarkOrchid4", bg="lavender blush", font="Verdana")
self.results.pack()
self.results.grid(column=0, row=0)
def showup(x):
return lambda: self.results.insert(END, x)
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for i in range(9):
n=numbers[i]
Button(buttons, bg="snow", text=n, width=7, height=1, command=showup(n), relief=RAISED).grid(row=i%3, column=i//3)
Clear = Button(buttons, bg="snow", text="C", width=7, height=1, command=self.clear, relief=RAISED)
Clear.grid(padx=2, pady=2, column=3, row=0)
Equals = Button(buttons, bg="snow", text="=", width=7, height=1, command=self.equals, relief=RAISED)
Equals.grid(padx=2, pady=2, column=4, row=3)
All_clear = Button(buttons, bg="snow", text="AC", width=7, height=1, command=self.all_clear, relief=RAISED)
All_clear.grid(padx=2, pady=2, column=4, row=0)
Bracket_one = Button(buttons, bg="snow", text="(", width=7, height=1, command=self.bracket_one, relief=RAISED)
Bracket_one.grid(padx=2, pady=2, column=2, row=3)
Bracket_two = Button(buttons, bg="snow", text=")", width=7, height=1, command=self.bracket_two, relief=RAISED)
Bracket_two.grid(padx=2, pady=2, column=3, row=3)
Zero = Button(buttons, bg="snow", text="0", width=7, height=1, command=self.zero, relief=RAISED)
Zero.grid(padx=2, pady=2, column=0, row=3)
Decimal_point = Button(buttons, bg="snow", text=".", width=7, height=1, command=self.decimal_point, relief=RAISED)
Decimal_point.grid(padx=2, pady=2, column=1, row=3)
Multiplication = Button(buttons, bg="red", text="x", width=7, height=1, command=self.multiplication, relief=RAISED)
Multiplication.grid(padx=2, pady=2, column=3, row=1)
Division = Button(buttons, bg="powder blue", text="/", width=7, height=1, command=self.division, relief=RAISED)
Division.grid(padx=2, pady=2, column=4, row=1)
Addition = Button(buttons, bg="yellow", text="+", width=7, height=1, command=self.addition, relief=RAISED)
Addition.grid(padx=2, pady=2, column=3, row=2)
Subtraction = Button(buttons, bg="green", text="-", width=7, height=1, command=self.subtraction, relief=RAISED)
Subtraction.grid(padx=2, pady=2, column=4, row=2)
self.menubar = Menu(self)
def about():
messagebox.showinfo(title = "About", message = "Author")
return
self.helpMenu = Menu(self.menubar)
self.menubar.add_cascade(label="Help",menu=self.helpMenu)
self.helpMenu.add_command(label = "About", command=about)
calculator.config(menu=self.menubar)
def equals(self):
try:
result = eval(self.results.get())
except:
result = "Invalid input"
self.all_clear()
self.results.insert(0, result)
def zero(self):
self.results.insert(END, "0")
def bracket_one(self):
self.results.insert(END, "(")
def bracket_two(self):
self.results.insert(END, ")")
def all_clear(self):
self.results.delete(0, END)
def clear(self):
self.results.delete(-1)
def multiplication(self):
self.results.insert(END, "*")
def division(self):
self.results.insert(END, "/")
def addition(self):
self.results.insert(END, "+")
def subtraction(self):
self.results.insert(END, "-")
def decimal_point(self):
self.results.insert(END, ".")
if __name__ == '__main__':
Calculator().mainloop()
calculator.config(menu=menubar)
calculator.mainloop()