I am trying to create a simple hangman game using tkinter with python. While creating the GUI, the first thing I put in were the dashes.lbl = tkinter.Label(window, text= xhash). When I put in my buttons, the label disappeared. I have tried making the window bigger, but it still didn't come back.
Here is my full code:
#importing modules
import tkinter
import random
#creating the dashes
f = open(r'C:\Users\Gareth\Documents\hangman\words.txt', 'r')
word = f.readlines()
guess = random.choice(word)
hash = []
xhash = ""
while len(hash) < len(str(guess)):
hash.append('_ ')
def rejoin():
xhash = "".join(hash)
xhash.strip("[]")
xhash.strip(",")
xhash.strip("'")
#creating the window
window = tkinter.Tk()
window.minsize(300, 300)
#commands for buttons a-z
def acmd():
for i in list(guess):
if i == 'a':
hash[guess.index(i)] = 'a'
rejoin()
lbl.configure(text= xhash)
abtn.configure(bg = 'red')
#label
lbl = tkinter.Label(window, text= xhash)
#buttons for letters a-z
abtn = tkinter.Button(window, text = 'a', command = acmd)
bbtn = tkinter.Button(window, text = 'b')
cbtn = tkinter.Button(window, text = 'c')
dbtn = tkinter.Button(window, text = 'd')...
#placing lbl and buttons
lbl.grid(row = 1, column = 1)
abtn.grid(row = 3 , column = 1)
bbtn.grid(row = 3 , column = 2)
cbtn.grid(row = 3 , column = 3)...
window.mainloop()
You would need to position them correctly, most probably either you are not positioning them or positioning them on top of each other. You would need to use grid() to position them
Example -
lbl = tkinter.Label(window, text= xhash)
lbl.grid(row=0,column=1)
abtn = tkinter.Button(window, text = 'a', command = acmd)
abtn.grid(row=0,column=2)
The problem did not lie within the label, but within the text.
def rejoin():
xhash = "".join(hash)
xhash.strip("[]")
xhash.strip(",")
xhash.strip("'")
the only adjustment needed was a global statement...
without that, whatever was done in the function had nothing to do with anything else, so, the previously assigned xhash = '', was simply left blank...
Related
I am currently working on a hangman game using tkinter in python.
When I click a button of the letter and it is in the word that we are guessing it should show the letter. But when I click the button this problem is popping up:
This example is only with one button. People say that this problem is because of the mainloop(), but i have no idea how to fix it.
from tkinter import *
from tkinter import messagebox
from generate_word import word
#DEFAULT VALUES
score = 0
count = 0
win_count = 2
WINDOW_BG = '#e5404e'
WINDOW_SIZE = '1200x870+300+80'
FONT = ('Arial', 40)
from tkinter import *
from tkinter import messagebox
from generate_word import word
#DEFAULT VALUES
score = 0
count = 0
win_count = 2
WINDOW_BG = '#e5404e'
WINDOW_SIZE = '1200x870+300+80'
FONT = ('Arial', 40)
#this is an example with only one button
buttons = [['b1','a',80,740]]
#Creating window and configurating it
window = Tk()
window.geometry(WINDOW_SIZE)
window.title('Hangman')
window.config(bg = WINDOW_BG)
#generates all of the labels for the word
def gen_labels_word():
label = Label(window, text = " ", bg = WINDOW_BG, font = FONT)
label.pack( padx = 40,pady = (500,100),side = LEFT)
label1 = Label(window, text = word[0], bg = WINDOW_BG, font = FONT)
label1.pack( padx = 41,pady = (500,100),side = LEFT)
x = 21
for var in range(1,len(word)):
exec('label{}=Label(window,text="_",bg=WINDOW_BG,font=FONT)'.format(var))
exec('label{}.pack(padx = {}, pady = (500,100), side=LEFT)'.format(var,x))
x += 1
exec('label{} = Label(window, text = "{}", bg = WINDOW_BG, font = FONT)'.format(len(word),word[-1]))
exec('label{}.pack( padx = {},pady = (500,100),side = LEFT)'.format(len(word), x+1))
# ---------------------------------------------------------------------------------------------------------------------------------------------------
gen_labels_word()
#----------------------------------------------------------------------------------------------------------------------------------------------------
#letters icons(images)
#hangman (images)
hangman = ['h0','h1','h2','h3','h4','h5','h6']
for var in hangman:
exec(f'{var}=PhotoImage(file="{var}.png")')
han = [['label0','h0'],['label1','h1'],['label2','h2'],['label3','h3'],['label4','h4'],['label5','h5'],['label6','h6']]
for p1 in han:
exec('{}=Label(window, bg = WINDOW_BG ,image={})'.format(p1[0],p1[1]))
exec('label0.place(x = 620,y = 0)')
for var in letters:
exec(f'{var}=PhotoImage(file="{var}.png")')
for var in buttons:
exec(f'{var[0]}=Button(window,bd=0,command=lambda: game_brain("{var[0]}","{var[1]}"),bg = WINDOW_BG,font=FONT,image={var[1]})')
exec('{}.place(x={},y={})'.format(var[0],var[2],var[3]))
def game_brain(button, letter):
global count,win_count,score
exec('{}.destroy()'.format(button))
if letter in word:
for i in range(1,len(word)):
if word[i] == letter:
win_count += 1
exec(f'label{i}.config(text="{letter}")')
if win_count == len(word):
score += 1
messagebox.showinfo('GOOD JOB, YOU WON!\n GOODBYE!')
window.destroy()
else:
count += 1
exec('label{}.destroy()'.format(count-1))
exec('label{}.place(x={},y={})'.format(count,620,0))
if count == 6:
messagebox.showinfo('GAME OVER','YOU LOST!\nGOODBYE!')
window.destroy()
def EXIT():
answer = messagebox.askyesno('ALERT','Do you want to exit the game?')
if answer == True:
window.destroy()
e1 = PhotoImage(file = 'exit.png')
ex = Button(window,bd = 0,command = EXIT,bg = WINDOW_BG,font = FONT,image = e1)
ex.place(x=1050,y=20)
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
window.mainloop()
Why exec function in python adds '.!' at the beginning of the text?
The exec function isn't doing that. Tkinter by default names all of its widgets with a leading exclamation point. When you print out a widget, it has to be converted to a string. For tkinter widgets, the result of str(some_widget) is the widget's name.
You can see this quite easily without exec:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root)
print(label)
The above will print something like .!label. If you create a second label it will be .!label2, a third will be .!label3 and so on.
On an unrelated note, you shouldn't be using exec to create widgets. It makes the code very hard to understand and debug. If you want to create widgets in a loop, add them to a dictionary or list instead of dynamically creating variables with exec.
For example:
labels = {}
for var in range(1,len(word)):
label = Label(window,text="_",bg=WINDOW_BG,font=FONT)
label.pack(padx=500, pady=100)
labels[var] = label
With that, you can later reference the widgets as labels[1], labels[2], etc.
You should do the same thing with the images, or anything else that you create in a loop and want to keep track of.
import random
import tkinter as tk
frame = tk.Tk()
frame.title("koeweils baldadige encyptor")
frame.geometry('400x200')
printButton = tk.Button(frame,text = "Print", command = lambda: zandkasteel())
printButton.pack()
freek = tk.Text(frame,height = 5, width = 20)
freek.pack()
input_a = freek.get(1.0, "end-1c")
print(input_a)
fruit = 0
fad = input_a[fruit:fruit+1]
print(fad)
schepje = len(input_a.strip("\n"))
print(schepje)
def zandkasteel():
lbl.config(text = "Ingevulde string: "+input_a)
with open("luchtballon.txt", "w") as chocoladeletter:
for i in range(schepje):
n = random.randint()
print(n)
leuk_woord = ord(fad)*n
print(leuk_woord)
chocoladeletter.write(str(leuk_woord))
chocoladeletter.write(str(n))
chocoladeletter.write('\n')
lbl = tk.Label(frame, text = "")
lbl.pack()
frame.mainloop()
I need to get the string that was entered into the text entry field freek. I have tried to assign that string to input_a, but the string doesn't show up.
Right now, input_a doesn't get anything assigned to it and seems to stay blank. I had the same function working before implementing a GUI, so the problem shouldn't lie with the def zandkasteel.
To be honest I really don't know what to try at this point, if you happen to have any insights, please do share and help out this newbie programmer in need.
Here are some simple modifications to your code that shows how to get the string in the Text widget when it's needed — specifically when the zandkasteel() function gets called in response to the user clicking on the Print button.
import random
import tkinter as tk
frame = tk.Tk()
frame.title("koeweils baldadige encyptor")
frame.geometry('400x200')
printButton = tk.Button(frame, text="Print", command=lambda: zandkasteel())
printButton.pack()
freek = tk.Text(frame, height=5, width=20)
freek.pack()
def zandkasteel():
input_a = freek.get(1.0, "end-1c")
print(f'{input_a=}')
fruit = 0
fad = input_a[fruit:fruit+1]
print(f'{fad=}')
schepje = len(input_a.strip("\n"))
print(f'{schepje=}')
lbl.config(text="Ingevulde string: " + input_a)
with open("luchtballon.txt", "w") as chocoladeletter:
for i in range(schepje):
n = random.randint(1, 3)
print(n)
leuk_woord = ord(fad)*n
print(leuk_woord)
chocoladeletter.write(str(leuk_woord))
chocoladeletter.write(str(n))
chocoladeletter.write('\n')
lbl = tk.Label(frame, text="")
lbl.pack()
frame.mainloop()
I'm wondering if I got my if else statement wrong or if its a tkinter issue. I want it so that if a 0 is left in any or all boxes, it gives an error message. But after the error message is closed, it opens a random blank window. This is my code. The specific area is the if else statement within the function valueget()
import tkinter as tk
def mainwindow():
mainwindow = tk.Tk()
mainwindow.title('Enter values')
mainwindow.geometry('160x110')
mainwindow.config(bg='#aaf0d1')
tk.Label(mainwindow, text = 'Enter a', font = ('verdana'), bg='#aaf0d1').grid(row=0)
tk.Label(mainwindow, text = 'Enter b', font = ('verdana'), bg='#aaf0d1').grid(row=1)
tk.Label(mainwindow, text = 'Enter c', font = ('verdana'), bg='#aaf0d1').grid(row=2)
getA = tk.IntVar()
aBox = tk.Entry(mainwindow, textvariable = getA, width=3, bg='#aaf0d1')
aBox.grid(row=0, column=1)
aBox.config(highlightbackground='#aaf0d1')
getB = tk.IntVar()
bBox = tk.Entry(mainwindow, textvariable = getB, width=3, bg='#aaf0d1')
bBox.grid(row=1, column=1)
bBox.config(highlightbackground='#aaf0d1')
getC = tk.IntVar()
cBox = tk.Entry(mainwindow, textvariable = getC, width=3, bg='#aaf0d1')
cBox.grid(row=2, column=1)
cBox.config(highlightbackground='#aaf0d1')
button = tk.Button(mainwindow, text='Obtain roots', command = lambda: valueget(), font = ('verdana'), highlightbackground='#aaf0d1')
button.grid(row=4)
button.config(bg='#aaf0d1')
def valueget():
readA = getA.get()
readB = getB.get()
readC = getC.get()
intA = int(readA)
intB = int(readB)
intC = int(readC)
negroot = (readB**2)-(4*readA*readC)
quadformulaplus = (-readB + (pow(negroot,0.5)))/(2*readA) #quad forumla
quadformulaminus = (-readB - (pow(negroot,0.5)))/(2*readA) #quad forumla
messagewindow = tk.Tk()
messagewindow.geometry('290x50')
messagewindow.title('Roots of the equation')
messagewindow.config(bg='#aaf0d1')
if readA == 0 or readB==0 or readC==0 or (readA==0 and readB==0 and readC==0):
errorwindow = tk.messagebox.showerror(message='none').pack()
else:
label = tk.Label(messagewindow, text = f'The roots are {quadformulaplus:.1f} and {quadformulaminus:.1f}', bg='#aaf0d1', font = ('verdana'))
label.grid(row=1)
closebutton = tk.Button(messagewindow, text='Close', command = lambda: messagewindow.destroy(), font = ('verdana'), highlightbackground='#aaf0d1')
closebutton.grid(row=2)
closebutton.config(bg='#aaf0d1')
messagewindow.mainloop()
# print(f'the roots are {quadformulaplus:.1f} and {quadformulaminus:.1f}')
mainwindow.mainloop()
def startup():
startpage = tk.Tk()
startpage.title('Solver')
photo = tk.PhotoImage(file = r"/Users/isa/Desktop/DiffEqns/cover.png") #image load
coverbutton = tk.Button(startpage, image = photo, command = lambda: [startpage.destroy(), mainwindow()])
coverbutton.pack()
coverbutton.configure(highlightbackground='#aaf0d1')
startpage.mainloop()
startup()
Here's a basic idea of what I would do:
import tkinter as tk
from tkinter import messagebox
def mainwindow(root):
# Creates a toplevel window
mainwindow = tk.Toplevel()
mainwindow.protocol("WM_DELETE_WINDOW", root.destroy) # This overrides the "X" being clicked to also destroy the root window.
root.withdraw() # "Hides" the root window, leaving it (and mainloop) running in the background.
mainwindow.title('Enter values')
mainwindow.geometry('160x110')
mainwindow.config(bg='#aaf0d1')
# Since all three of the labels/entries are the same
# we can save space by generating them in a loop
entry_items = ('Enter a', 'Enter b', 'Enter c')
values = []
for x, item in enumerate(entry_items): # Using enumerate and x to assign rows
tk.Label(mainwindow, text = item,
font = ('verdana'), bg='#aaf0d1').grid(row=x) # Row assigned to x.
values.append(tk.StringVar()) # Appended StringVar to list.
tk.Entry(mainwindow,
textvariable = values[-1], # Uses the last value appended to the values list.
highlightbackground='#aaf0d1',
width=3,
bg='#aaf0d1').grid(row=x, column=1) # Row assigned to x.
tk.Button(mainwindow,
text='Obtain roots',
command = lambda vals = values: valueget(vals), # Here the button command is assigned with the values list
font = ('verdana'), bg='#aaf0d1',
highlightbackground='#aaf0d1').grid(row=3) # we know there are 3 items before this.
mainwindow.lift() # This is a method of bringing a window to the front
def valueget(vals):
# This line gets the values from the StringVars, converts them to ints,
# and returns them to their respective variables.
try:
readA, readB, readC = [int(val.get()) for val in vals]
except ValueError:
messagebox.showerror(title="Number Error", message='Values must be numbers')
return
# Here the variables are checked to see if they are 0
# Since each one is being checked if it is 0, there is no need to check if they are all 0.
for val in (readA, readB, readC):
if val == 0:
# If they are 0, shows an error message
messagebox.showerror(title="Zero Error", message='Values must not be zero')
return
# Creates a toplevel to display the results
messagewindow = tk.Toplevel()
messagewindow.title('Roots of the equation')
messagewindow.config(bg='#aaf0d1')
negroot = (readB**2)-(4*readA*readC)
quadformulaplus = (-readB + (pow(negroot,0.5)))/(2*readA) #quad forumla
quadformulaminus = (-readB - (pow(negroot,0.5)))/(2*readA) #quad forumla
tk.Label(messagewindow,
text = f'The roots are {quadformulaplus:.1f} and {quadformulaminus:.1f}',
bg='#aaf0d1',
font = ('verdana')).pack(padx = 5, pady = 2)
tk.Button(messagewindow,
text='Close',
command = messagewindow.destroy, # There is no need for a lambda for this.
font = ('verdana'),
bg = '#aaf0d1',
highlightbackground='#aaf0d1').pack(padx = 5, pady = 2)
# print(f'the roots are {quadformulaplus:.1f} and {quadformulaminus:.1f}')
messagewindow.lift() # This is a method of bringing a window to the front
def startup():
startpage = tk.Tk()
startpage.title('Solver')
# COMMENTED OUT FOR TESTING
#photo = tk.PhotoImage(file = r"/Users/isa/Desktop/DiffEqns/cover.png") #image load
coverbutton = tk.Button(startpage,
# COMMENTED OUT FOR TESTING
#image = photo,
text = "TESTING", # HERE FOR TESTING
highlightbackground='#aaf0d1',
command = lambda root = startpage: mainwindow(root)).pack() # Passes the startpage to the mainwindow function.
startpage.mainloop() # The only mainloop you need.
startup()
I would recommend to improve the readability of the if-else statement for a start.
coefficients = [readA, readB, readC]
if sum(coefficients): # If they all are all zeros this will be False
if min(coefficients): # If any one is zero, this will be False
label = tk.Label(messagewindow, text = f'The roots are {quadformulaplus:.1f} and {quadformulaminus:.1f}', bg='#aaf0d1', font = ('verdana'))
label.grid(row=1)
closebutton = tk.Button(messagewindow, text='Close', command = lambda: messagewindow.destroy(), font = ('verdana'), highlightbackground='#aaf0d1')
closebutton.grid(row=2)
closebutton.config(bg='#aaf0d1')
else:
errorwindow = tk.messagebox.showerror(message='none').pack()
else:
errorwindow = tk.messagebox.showerror(message='none').pack()
I am trying to create a GUI that will be able to monitor the integrity of files using their MD5 hashes (the actual monitoring of updates log can be in the command prompt).
I got the initial command line program to work perfectly, but am having an issue when converting it to a GUI based version using tkinter.
I use the GUI to create a list of files that I want to monitor in the 'addFiles' function, but when I try to pass that list to the 'checkForIntegrity' function (or print the list with my test print(listOfFiles) code in that function), all I get is [tkinter.StringVar object at 0x01FEAD50], but do not get the actual list.
I have searched far and wide for an answer and have tried using various implementations of 'listOfFiles.get()' in different locations but have had no success.
I have no idea why I only get the actual list object but no listed items, my code is below.
Thank you in advance everyone.
edit: Just to be clear, my 'GUI()' function creates a window that asks how many files the user would like to monitor and passes that to the 'addFiles()' function which allows input for the amount of files they specify. I need to be able to pass the files they specify in that GUI to the program via a list. Thanks again.
import hashlib
import time
from tkinter import *
def main():
GUI()
def GUI():
window = Tk()
window.title("Integrity Checker")
frame1 = Frame(window)
frame1.pack()
label1 = Label(frame1, text = "***Proof Of Concept Program That Monitors the Integriry of Files***")
label1.grid(row = 1, column = 1)
frame2 = Frame(window)
frame2.pack()
getNumberOfFiles = Label(frame2, text = "Insert Number of Files You Would Like to Check: ")
getNumberOfFiles.grid(row = 2, column = 1)
numberOfFiles = IntVar()
NumberOfFilesOption = Entry(frame2, textvariable = numberOfFiles)
NumberOfFilesOption.grid(row = 2, column = 2)
button = Button(frame2, text = "OK", command = lambda : addFiles(numberOfFiles))
button.grid(row = 2, column = 3)
window.mainloop()
def addFiles(numberOfFiles):
listOfFiles = []
window = Tk()
window.title("Add Files")
frame1 = Frame(window)
frame1.pack()
label1 = Label(frame1, text = "***Select The Files You Want To Monitor***")
label1.grid(row = 1, column = 1)
for i in range (numberOfFiles.get()):
AddFile = Label(frame1, text = "Add File:")
AddFile.grid(row = (i + 3), column = 1)
FileName = StringVar()
FileNameOption = Entry(frame1, textvariable = FileName)
FileNameOption.grid(row = (i + 3), column = 2)
button = Button(frame1, text = "OK", command = lambda : listOfFiles.append(FileName))
button.grid(row = (i + 3), column = 3)
button2 = Button(frame1, text = "Done", command = lambda : checkforINTEGRITY(numberOfFiles, listOfFiles))
button2.grid(row = (i + 4), column = 2)
window.mainloop()
def checkforINTEGRITY(numberOfFiles, listOfFiles):
#Number = numberOfFiles.get()
#listOfFiles = []
#count = 0
#numberOfFiles = eval(input("How many files would you like to monitor?: "))
#while count < Number:
# filename = input("Enter the name of the file you would like to check: ")
# count += 1
# listOfFiles.append(filename)
print(listOfFiles)
i = 0
originalList = []
for file in listOfFiles:
original_md5 = hashlib.md5(open(listOfFiles[i],'rb').read()).hexdigest()
originalList.append(original_md5)
i += 1
print(originalList)
while True:
i = 0
while i < Number:
md5_returned = hashlib.md5(open(listOfFiles[i],'rb').read()).hexdigest()
print(md5_returned)
if originalList[i] == md5_returned:
print("The file", listOfFiles[i], "has not changed")
else:
print("The file", listOfFiles[i], "has been modified!")
i += 1
time.sleep(5)
main()
It looks like you want to call the get method on FileNameOption:
button = Button(frame1, text = "OK", command = lambda : listOfFiles.append(FileNameOption.get()))
With this change I was able to get a list of strings in listOfFiles.
I'm having a major issue with my code below. So here's what I'm trying to do.
The user enters certain inputs into the entry fields and the program calculates and displays a corresponding image (loaded in the frame champFrame) and it's description (in spellFrame).
What I want is to replace this image and text every time I click on the Calculate! button so that it shows me a new image and new description based on my new inputs.
However, in its current state, when I enter new input and click the Calculate! button, it just adds the new image below the old one and the old one is still there.
How do I change my code around so that I can replace these old values?
I have heard of using the pack_forget() method but it only hides the previous image, does not remove it so I can replace it with a new one.
from Tkinter import *
from PIL import ImageTk
from Calculation import getHighestScore
from ImageHandler import *
import json
import urllib2
top = Tk()
top.title("LOLSpellEfficiency")
top.geometry("600x600")
def loadInputFields(frame):
AP_Label = Label(frame, text="AP")
AD_Label = Label(frame, text="AD")
CDR_Label = Label(frame, text="CDR")
AP_Label.grid(row=0, column=0)
AD_Label.grid(row=1, column=0)
CDR_Label.grid(row=2, column=0)
AP_Entry = Entry(frame)
AD_Entry = Entry(frame)
CDR_Entry = Entry(frame)
AP_Entry.grid(row=0, column=1)
AP_Entry.insert(0, "0")
AD_Entry.grid(row=1, column=1)
AD_Entry.insert(0, "0")
CDR_Entry.grid(row=2, column=1)
CDR_Entry.insert(0, "0")
return [AP_Entry, AD_Entry, CDR_Entry]
def loadButton(frame, entries):
Enter_Button = Button(frame, text="Calculate!", command = lambda: executeProgram(entries))
Enter_Button.pack(side=TOP)
def executeProgram(entries):
AP = float(entries[0].get())
AD = float(entries[1].get())
CDR = float(entries[2].get())
data = getChampionData()
result = getHighestScore(data, AP, AD, CDR)
champPhoto = ImageTk.PhotoImage(getChampionImage(result[0]))
champLabel = Label(championFrame, image=champPhoto)
champLabel.image = champPhoto
champLabel.pack(side = TOP)
champName = Label(championFrame, text = "Champion: " + result[0])
champName.pack(side = BOTTOM)
spellPhoto = ImageTk.PhotoImage(getSpellImage(result[2]))
spellLabel = Label(spellFrame, image=spellPhoto)
spellLabel.image = spellPhoto
spellText = Text(spellFrame, wrap=WORD)
spellText.insert(INSERT, "Spell: " + result[1] + "\n")
spellText.insert(INSERT, "Spell description: " + result[3] + "\n")
spellLabel.pack(side=TOP)
spellText.pack(side=BOTTOM)
championFrame.pack()
spellFrame.pack()
def getChampionData():
URL = "https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion?champData=all&api_key=8e8904b4-c112-4b0f-bd1e-641649d9e569"
return json.load(urllib2.urlopen(URL))
inputFrame = Frame(top)
inputFrame.pack()
buttonFrame = Frame(top)
buttonFrame.pack()
championFrame = Frame(top)
spellFrame = Frame(top)
championFrame.pack()
spellFrame.pack()
entries = loadInputFields(inputFrame)
loadButton(buttonFrame, entries)
top.mainloop()
Do not create widgets inside of executeProgram. Instead, create them in your main application, and simply change the widgets inside executeProgram.
All widgets have a configure method which lets you change all of the attributes of that widget. For example, to change the image on a label you would do something like this:
champPhoto = ImageTk.PhotoImage(getChampionImage(result[0]))
champLabel.configure(image=champPhoto)
champLabel.image = champPhoto