I'm relatively new when it comes to python and even more new when it comes to Tkinter, and I am making a Dice game program using GUIs and functions. At the end of the game, a new window pops up and displays some text with an output Widget that is meant to display the score, however, it doesn't appear. I believe I am using the right syntax, and there are no error messages when I run the program. Any answer would be appreciated.
Here is my code below:
'''
# SPICEY DICE Code snippet.
# - Create window
window2 = Tk()
window2.title("SPICEY DICE - Single Player Finish")
# - Label 1
Label(window2, text = "CONGRATULATIONS!").grid(row = 0, column = 0, sticky = W)
# - Label 2
Label(window2, text = "You have finished SPICEY DICE with a score of:").grid(row = 1, column = 0, sticky = W)
# - Text box 1
Output5 = Text(window2, width = 5, height = 1, wrap = WORD, background = "yellow")
Output5.grid(row = 2, column = 0, sticky = W)
# - Button 1
Button(window2, text = "LEADERBOARD", width = 15, command = SPLeaderboard).grid(row = 3, column = 0, sticky = W)
# - Label 3
Label(window2, text = "Close all of the windows to finish.").grid(row = 4, column = 0, sticky = W)
# - Run mainloop
window2.mainloop()
Output5.delete(0.0, END)
Output5.insert(END, str(TotalScore))
'''
The Output widget in question is 'Output5' and 'TotalScore' is the variable that is meant to be printed into it but doesn't appear.
Many Thanks.
I guess you need to write:
Output5.delete(0.0, END)
Output5.insert(END, str(TotalScore))
these codes between Tk() and mainloop.
Related
I'm currently creating a counter application, similar to those on checkout sections in an e-commerce platform. I was trying to implement a feature, where no negative integers can occur.
One way I thought about doing this was by disabling the state of the minus button when the value of the input field is 0.
here's the code:
# from tkinter, import everything
from tkinter import *
#set up main window
root = Tk()
#edit window tab name
#can we edit length of title so there's less space of the - square and cross button
root.title("Counter")
#set up root geometry
root.geometry("210x50")
#main code
#input box for numbers display
input_box = Entry(root, width = 8)
input_box.grid(row = 3 , column = 10, sticky = "nsew")
#start the input box at a default value of 0
input_box.insert(0,0)
#plus widget
plus_button = Button(root, text = "+", width = 2 , height = 1, command = add)
plus_button.grid(row = 5 , column = 15, sticky = "nsew")
#minus widget
minus_button = Button(root, text = "-", width = 2, height =1, command = subtract , state = DISABLED)
minus_button.grid(row = 5, column = 8, sticky = "nsew" )
#resizing windows dynamically
Grid.rowconfigure(root, index =3, weight =1)
Grid.rowconfigure(root, index = 5, weight =1)
Grid.rowconfigure(root, index = 5, weight =1)
Grid.columnconfigure(root,index = 8, weight=1)
Grid.columnconfigure(root,index = 10, weight=1)
Grid.columnconfigure(root,index = 15, weight=1)
#functions
def add():
#get the value stored in the input_box and store into variable(convert it into int)
input_box_value = int(input_box.get())
#increment the value
input_box_value += 1
#delete content in input_box
input_box.delete(0,"end")
#insert the updated input_box_value
input_box.insert(0,input_box_value)
def subtract():
#to modify buttons, it uses indexing technique, similar to normal python
#you treat the content in the [] as a string and what you want to change to as a string
#get the current value from input box and convert to int
input_box_value = int(input_box.get())
if input_box_value != 0:
minus_button.config(state = NORMAL)
#decrease input_box_value by 1
input_box_value -= 1
#delete the current content in the box
input_box.delete(0,"end")
#place the updated input_box_value
input_box.insert(0,input_box_value)
#set up application loop for main window
root.mainloop()
The issue I run into is that when I run the program, the minus button is initialized to disabled (which is expected), but once I increase the value from 0 [say 1,2,3 etc.], the minus button still remains disabled, even though I want it to become normal.
My guess is that maybe something is wrong with how I inserted the value of 0, or how I worked with .get(). Instead of this approach, I was considering using stringvar, but not sure about it.
Thanks, Safwan
I'd like to switch from the rating_frame to the summary_frame. How would I do this? Would I destroy the rating_frame? I want to go onto the rating_frame by clicking the 'Show' button.
I have a search frame that is staying there. I only want the rating frame to change.
I have not yet made a start on the summary_frame as I don't know how to change from the rating_frame or where I would write it. Could you give me a good foundation?
Here is my Wireframe:
Here is my code:
from tkinter import *
class Movie:
def __init__(self, movie):
self.movie = movie
self.ratings = "No Rating"
class MovieRaterGUI:
def __init__(self, parent):
self.counter = 0
self.index = 0
#variable set up
self.v = StringVar()
self.v.set("No Rating")
#frames used so you can easily switch between rating frame and summary frame - keeping the search frame
rating_frame = Frame(root)
search_frame = Frame(root)
summary_frame = Frame(root)
rating_frame.pack(side="top", expand=True)
search_frame.pack(side="bottom", expand=True)
summary_frame.pack(side="top", expand = True)
#rating frame
#list of ratings for movies
self.movies = [
Movie("The Hobbit"),
Movie("Coraline"),
Movie("Love, Rosie")]
#used to display the ratings
self.ratings = ["No Rating", "Forget it", "2", "3", "4", "Must See"]
#labels
self.movie_label = Label(rating_frame, text = "Please Rate:", borderwidth = 10)
self.current_movie = Label(rating_frame, text = self.movies[self.counter].movie, borderwidth = 10)
self.rating_label = Label(rating_frame, text = "Your rating:", borderwidth = 10)
self.movie_label.grid(row = 0, column = 0, sticky = W)
self.current_movie.grid(row = 0, column = 1, sticky = W)
self.rating_label.grid(row = 1, column = 0, sticky = W)
#making radio buttons
self.radiobutton = []
self.num_choices = self.ratings
for i in range(len(self.ratings)):
self.option = Radiobutton(rating_frame, variable = self.v, value = self.ratings[i], text = self.ratings[i], borderwidth = 10, command = self.update_rating)
self.radiobutton.append(self.option)
self.option.grid(row = i+1, column = 1, sticky = W)
next_btn = Button(rating_frame, text = "Next", borderwidth = 10, command = self.next_movie)
previous_btn = Button(rating_frame, text = "Previous", borderwidth = 10, command = self.previous_movie)
next_btn.grid(row = 7, column = 1, sticky = W)
previous_btn.grid(row = 7, column = 0, sticky = W)
#search frame
self.search_label = Label(search_frame, text = "Search for movies with a rating of:", borderwidth = 10)
self.search_label.grid(row=0, column=0, columnspan=len(self.num_choices))
for i in range(len(self.num_choices)):
option = Radiobutton(search_frame, variable = self.v, value = i, text = self.num_choices[i])
option.grid(row = 1, column = i, sticky = W)
show_btn = Button(search_frame, text = "Show", borderwidth = 10, command = self.summary_frame)
show_btn.grid(row = 3, column = 0, columnspan = len(self.num_choices))
def next_movie(self):
self.counter +=1
self.current_movie.configure(text = self.movies[self.counter].movie)
#used so each radio button the user chooses will be saved
for i in range(len(self.radiobutton)):
self.radiobutton[i].configure(variable = self.v, text = self.ratings[i], value = self.ratings[i])
#the default movie rating is no rating for every movie
self.v.set("No Rating")
def previous_movie(self):
self.counter -=1
self.current_movie.configure(text = self.movies[self.counter].movie)
#the default movie rating is no rating for every movie
self.v.set("No Rating")
def update_rating(self):
self.movies[self.counter].ratings = self.v.get()
for element in self.movies:
print(element.ratings)
print()
print('*'*20)
print()
if __name__ == "__main__":
root = Tk()
root.title("Movie Ratings")
radiobuttons = MovieRaterGUI(root)
root.mainloop()
If you use destroy then you destroy also data which you have in this frame - and you couldn't use them. You would have to get data from frame before you destroy it.
But it may be better to remove frame without destroying (pack_forget, grid_forget) and then you have access to data in frame and you can always display this frame again.
With grid it can be simpler to put new element in the same place.
Where put this code?
Usually programs have buttons << Previous,Next >> or << Details, Summary >> to change frames/steps - but it seems you forgot these buttons.
Eventually you can use Notebook to have frames in tabs.
Applications for smartphones usually can slide frames (using fingers) but desktop programs rather don't use this method. And tkinter doesn't have special methods for this. It would need much more code with events or drag'&'drop. Buttons are much simpler to create and simpler to use by users.
I am helping with a small project where we want to add and take items away from a store. The code is below:
from tkinter import *
import tkinter
####################
# Variables
eggs = 0
milk = 0
butter = 0
lemon = 0
guiSize = "800x1280"
def newItemGUI():
main.withdraw()
def addEgg():
global eggs
eggs += 1
updateLabels()
def menu():
global eggs
update(eggs)
itemWindow.destroy()
main.deiconify()
def updateLabels():
eggLabel = Label(itemWindow, text = eggs)
eggLabel.grid(row = 3,column = 0)
itemWindow = Toplevel()
itemWindow.geometry(guiSize)
itemWindow.title("Add a new item")
itemWindow.configure(background = "#a1dbcd")
heading = Label(itemWindow, text="Add new items", font=("Arial",20),background = "#a1dbcd")
heading.grid(row=0, columnspan = 3)
egg = PhotoImage(file ="images/egg.gif")
eggButton = Button(itemWindow, image = egg, command = addEgg)
eggButton.grid(row = 2, column = 0,padx = 10, pady = 10)
eggLabel = Label(itemWindow, text = eggs).grid(row = 3,column = 0)
back = Button(itemWindow, text = "Main Menu", command = menu, width = 15)
back.grid(row = 4, column = 0, padx = 20, pady = 20)
def update(eggs):
items.delete("1.0",END)
items.insert(END,"Eggs \t:")
items.insert(END,eggs)
items.insert(END,"\n")
main=tkinter.Tk()
main.geometry(guiSize)
bgColour = "#DDA0DD"
main.configure(background = bgColour)
button1 = Button(main, text="Input new products", width = 20, height = 5, command = newItemGUI)
button1.grid(column = 1, row =2, padx = 20, pady = 20)
label2 = Label(main,text = "Items in the fridge :", font =("Arial,20"), background = bgColour)
label2.grid(row=4, columnspan = 2)
items = Text(main, width = 60, height = 10)
items.insert(END,"Eggs \t:")
items.insert(END,eggs)
items.insert(END,"\n")
items.grid(row=5, columnspan = 4)
main.mainloop()
When you click on the input new products button, this takes you to a new screen. On the screen should be a photo of an egg with a count underneath. For some reason the image of the egg is not showing and the button will not click.
If I change the eggButton from an image to:
eggButton = Button(itemWindow, text = "egg", command = addEgg)
this seems to allow me to click and the variable and it increases. Any idea as to what/where we have gone wrong? I know the path is correct as I can get the button to display a picture of an egg in a Label.
The problem is because the PhotoImage is being stored in a variable named egg which is local to the newItemGUI() function, so it (and associated image object) are being deleted when the function returns. This is a fairly common problem, so your question is likely a duplicate of another (and may get closed).
This PhotoImage documentation mentions avoiding this potential issue the way shown below in the Note near the end.
Regardless, to prevent that from happening, you can store the value somewhere else such as in an attribute of the Toplevel window. I would also recommend changing its name to something more descriptive like egg_image.
Here are changes to your code showing what how it could be done:
itemWindow.egg_image = PhotoImage(file="images/egg.gif")
eggButton = Button(itemWindow, image=itemWindow.egg_image, command = addEgg)
Recently I've changed the layout of my program to include a multi-page window similar to what is in the provided example.
In the original, two-window configuration I had a binding set on each window to highlight all of the text in the Entry widget, based on a condition (no condition present in the example). This was fine.
Upon upgrading to a multi-page window, I tried to combine the callback to highlight text by passing the relevant widget and calling widget.select_range(0, END) as it is done in the example. Now I can't seem to highlight any text on mouse-click.
In addition to this, I've also tested my example code with having a separate callback for each Entry; even this would not highlight the text in the Entry upon clicking on it.
Could this have something to do with lifting frames & where the focus lies? As a test I've added a similar callback for "submitting" the Entry value, and this is working fine. At this point I'm confused as to why this wouldn't work. Any help is greatly appreciated.
UPDATE:
I forgot that to solve the highlighting problem, I've needed to include a return "break" line in the callback that is used to highlight the text.
Now, with this included, I have some very strange behavior with the Entry widgets. I can't click on them unless they have been focused using the tab key.
Is there any way to work around this problem?
Here is the example code I have been playing with (with the updated return statement):
from Tkinter import *
class Window():
def __init__(self, root):
self.root = root
self.s1 = StringVar()
self.s1.set("")
self.s2 = StringVar()
self.s2.set("")
# Frame 1
self.f1 = Frame(root, width = 50, height = 25)
self.f1.grid(column = 0, row = 1, columnspan = 2)
self.page1 = Label(self.f1, text = "This is the first page's entry: ")
self.page1.grid(column = 0, row = 0, sticky = W)
self.page1.grid_columnconfigure(index = 0, minsize = 90)
self.val1 = Label(self.f1, text = self.s1.get(), textvariable = self.s1)
self.val1.grid(column = 1, row = 0, sticky = E)
self.l1 = Label(self.f1, text = "Frame 1 Label")
self.l1.grid(column = 0, row = 1, sticky = W)
self.e1 = Entry(self.f1, width = 25)
self.e1.grid(column = 1, row = 1, sticky = E)
self.e1.bind("<Button-1>", lambda event: self.event(self.e1))
self.e1.bind("<Return>", lambda event: self.submit(self.e1, self.s1))
# Frame 2
self.f2 = Frame(root, width = 50, height = 25)
self.f2.grid(column = 0, row = 1, columnspan = 2)
self.page2 = Label(self.f2, text = "This is the 2nd page's entry: ")
self.page2.grid(column = 0, row = 0, sticky = W)
self.page2.grid_columnconfigure(index = 0, minsize = 90)
self.val2 = Label(self.f2, text = self.s2.get(), textvariable = self.s2)
self.val2.grid(column = 1, row = 0, sticky = E)
self.l2 = Label(self.f2, text = "Frame 2 Label")
self.l2.grid(column = 0, row = 1, sticky = W)
self.e2 = Entry(self.f2, width = 25)
self.e2.grid(column = 1, row = 1, sticky = E)
self.e2.bind("<Button-1>", lambda event: self.event(self.e2))
self.e2.bind("<Return>", lambda event: self.submit(self.e2, self.s2))
self.b1 = Button(root, width = 15, text = "Page 1", command = lambda: self.page(1), relief = SUNKEN)
self.b1.grid(column = 0, row = 0, sticky = E)
# Buttons
self.b2 = Button(root, width = 15, text = "Page 2", command = lambda: self.page(2))
self.b2.grid(column = 1, row = 0, sticky = W)
# Start with Frame 1 lifted
self.f1.lift()
def page(self, val):
self.b1.config(relief = RAISED)
self.b2.config(relief = RAISED)
if val == 1:
self.f1.lift()
self.b1.config(relief = SUNKEN)
else:
self.f2.lift()
self.b2.config(relief = SUNKEN)
def event(self, widget):
widget.select_range(0, END)
return "break"
def submit(self, widget, target):
target.set(widget.get())
root = Tk()
w = Window(root)
root.mainloop()
Well, this has been a productive question. If anyone in the future is doing something similar to this and needs a reference for how to solve the problem:
I was able to work around the problem by forcing the Entry widgets into focus every time I switch frames, and using the return "break" statement that I mention in the question's update.
This isn't ideal, as every time a page is changed you automatically focus on the Entry widget, but once the widget is in focus it's behavior is exactly what I would expect so this isn't of great concern. In my program, if you are changing pages it is quite likely you will use the Entry widget anyway (it is a search entry).
Here's the final changes required to make the code work correctly:
# .... some code ....
self.f1.lift()
self.e1.focus_force()
def page(self, val):
self.b1.config(relief = RAISED)
self.b2.config(relief = RAISED)
if val == 1:
self.f1.lift()
self.b1.config(relief = SUNKEN)
self.e1.focus_force()
else:
self.f2.lift()
self.b2.config(relief = SUNKEN)
self.e2.focus_force()
def event(self, widget):
widget.select_range(0, END)
return "break"
# .... more code ....
I have the following code for my GUI:
root = Tk()
draft = Frame(root)
draft.grid()
root.title("test")
var = StringVar()
emailLabel = Label(draft, text="E-Mail:")
emailLabel.grid(row = 0, column = 0)
email = Entry(draft, justify=LEFT)
email.grid(row = 0, column = 1)
email.insert(0, "E-Mail")
passLabel = Label(draft, text="Pass:")
passLabel.grid(row = 1, column = 0)
password = Entry(draft, justify=LEFT, show="*")
password.grid(row = 1, column = 1)
password.insert(0, "Password")
start = Button(draft, text = "Start")
start.grid(row = 2, column = 0)
stop = Button(draft, text = "Stop")
stop.grid(row = 2, column = 1)
status = Text(draft)
status.grid(row = 3)
status.insert(INSERT, "TESTING")
However, it's not lining up the way I want it. I want the label and textboxes aligned to the left not the right, and the status text box to take up the entire size of the bottom (It's more or less a log).
Here's a screenshot:
To make the Text widget span over multiple columns, use the columnspan argument:
status.grid(row=3, columnspan=2)
Additionally, you can use the sticky parameter in the grid method to make the widgets occupy as many space as possible. Use this in every grid call:
widget.grid(..., sticky=N+W+E+S)
If you want the labels to be smaller, you have to rethink your layout a bit.
(Little note: it's not suggested to import * from tkinter. import tkinter as tk is preferred.)