How do I disable a window? - python

I am creating a ten question multiple choice quiz in Tkinter / Python. Essentially in the parent window there are 13 buttons - help button, user details, questions 1-10 and an 'End' button. Each button opens up a new window with the question, the options as checkbuttons and radiobuttons and an 'Enter' button which links to a piece of code that will calculate the correct answer and add 1 to the score if condition is true. Once the user has selected the answer and pressed 'Enter' the button will be disabled. However, once the user exits this window they are able to re answer the same question which will obviously result in multiple points being added to the global variable score. How do I disable the question button/window once the user has answered the question? And how do I make the 'Reveal Score' button only activated when all questions have been answered?
I have used a class to define each button and individual classes thereafter for each button so I am not sure if this will cause issues (I am new to Object Orientated). Thanks
I AM AWARE THAT INDENTATION IS NOT CORRECT, IHAD TO FORMAT IT FOR THIS WEBSITE
from Tkinter import * #Copied from online examples
import tkMessageBox #Copied from online examples
import Tkinter, Tkconstants, tkFileDialog #Copied from online examples
import Tkinter as tk #Copied from online examples
class Example(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.question_1_window = None
self.question_1 = tk.Button(self, text="1", foreground="blue", command=self.show_question_1)
self.question_1.pack(side="left")
def show_question_1(self):
'''show the question window; create it if it doesn't exist'''
if self.question_1_window is None or not self.question_1_window.winfo_exists():
self.question_1_window = Question_1_Window(self)
else:
self.question_1_window.flash()
class Question_1_Window(tk.Toplevel):
'''A simple instruction window'''
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
self.text = tk.Label(self, width=75, height=4, text = "1) Do you have the time to do at least twenty minutes of prefect duty each week?")
self.text.pack(side="top", fill="both", expand=True)
question_1_Var = IntVar() #creating a variable to be assigned to the radiobutton
Yes_1 = Radiobutton(self, text = "Yes", variable = question_1_Var, value=1, height=5, width = 20)
Yes_1.pack() #creating 'yes' option
#Here we are assigning values to each option which will be used in the validation
No_1 = Radiobutton(self, text = "No", variable = question_1_Var, value=2, height=5, width = 20)
No_1.pack() #creating 'no' option
def calculate_score_1():
Enter_1.config(state="disabled")
if (question_1_Var.get() == 1) and not (question_1_Var.get() == 2):
print("calculate score has worked") #test lines
parent.score_per_question[1] = 1
else:
print("not worked") #testlines
Enter_1 = Button(self, text= "Enter", width=10, command = calculate_score_1)
Enter_1.pack()
def flash(self):
'''make the window visible, and make it flash temporarily'''
# make sure the window is visible, in case it got hidden
self.lift()
self.deiconify()
# blink the colors
self.after(100, lambda: self.text.configure(bg="black", fg="white"))
self.after(500, lambda: self.text.configure(bg="white", fg="black"))
if __name__ == "__main__":
root = tk.Tk()
root.geometry("800x500") #defining the size of the root
root.title("Prefect Quiz") #defining root title
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()

If it creates a new window after than you can destroy it.
window.destroy()
"window" change depending on what you call your tkinter. TK
window = tkinter. TK
Whatever is before the = is what you replace "window" with.
hope that helped:)

Related

tkinter Button printing same thing multiple times

I am currently working on a little just for fun project, which pretends it´s generating something and then shows a specific message, but I have a question: As soon as I press the button on the screen it is showing a progressbar, that is what I want it to do, but if I press the button again it just shows the same thing again and again, is there any way to prevent the program from printing the Starting the generate text and the progressbar multiple times?
Here´s the code:
# my little import area
import tkinter as tk
from tkinter import ttk
# Initialization
win = tk.Tk()
win.title("StackOverflow")
# Window Size
win.resizable(False, False)
win.minsize(750,500)
# Button clicked command
def buttonclicked():
tk.Label(win, text="Starting to generate...").pack()
pb.pack()
pb.start(500)
#Widgets
headerlabel = tk.Label(win, text="StackOverFlow Question")
generatebutton = tk.Button(win, text="Generate", command=buttonclicked)
pb = ttk.Progressbar(win, orient="horizontal", length=250, mode="determinate")
#Positioning
headerlabel.pack()
generatebutton.pack()
win.mainloop()
You can put
global generatebutton
generatebutton.config(state='disabled')
in your buttonclicked function (which is stupid because the global keyword is usually SO BAD to use, it turns your code into nightmare), or use OOP to your advantage. You can also use win.destroy() or generatebutton.destroy().
Here is that more OOP-intensive code example:
import tkinter as tk
from tkinter import ttk
class Joke:
def __init__(self):
self.win = tk.Tk()
self.win.title("StackOverflow")
self.win.resizable(False, False)
self.win.minsize(750,500)
headerlabel = tk.Label(self.win, text="StackOverFlow Question")
self.generatebutton = tk.Button(self.win, text="Generate", command=self.buttonclicked)
self.pb = ttk.Progressbar(self.win, orient="horizontal", length=250, mode="determinate")
headerlabel.pack()
self.generatebutton.pack()
self.win.mainloop()
def buttonclicked(self):
tk.Label(self.win, text="Starting to generate...").pack()
self.pb.pack()
self.pb.start(500)
self.generatebutton.config(state='disabled')
Joke()
Hope that's helpful!

How to refer to a class list item?

I'm a beginner at Python and I don't know what to set command to so I can open one of the links in the class list (Sorry if I am calling it the wrong thing. Please include what to call it in your answer.) For example, if I wanted to open Slopes link, what would I type in the command for button_slope?
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
style = ttk.Style()
style.configure("TButton",
font="Serif 15",
padding=10)
class GameLibrary:
def __init__(self, game, link):
self.game = game
self.link = link
games = [
GameLibrary("Slope", "https://www.y8.com/games/slope"),
GameLibrary("Punch Boxing Championship", "https://www.y8.com/games/punch_boxing_championship"),
]
main_frame = Frame(root)
main_frame.pack()
main_frame.grid(row=0, columnspan=4)
button_slope = ttk.Button(main_frame, text='Slope', command='what do i type here').grid(row=1, column=0)
root.mainloop()
command should be set to a callback function that executes when the button is pressed. For instance.
def callback():
print "click!"
button_slope = ttk.Button(main_frame, text='Slope', command=callback)
button_slope.grid(row=1, column=0)
Will print click! when you click the button. You would want to take whatever action is appropriate for your program.

Tkinter store entry box value as value for other file

This is my first python personal project. I am wanting to use Tkinter to create a window (GUARDIAN LOCATOR) that asks the user to input a value (enter sailor guardian) into the entry box. The rest of the program is dependent on what the user types in the entry box as I will be having if/else statements reacting to the sailor guardian entered.
The issue I am having is storing what is entered in the entry box as a variable to use in my main file for the if/else statements. I can get the value to print to the prompt window, but I haven't been able to store it successfully to a global variable.
My Tkinter window is in it's own class.
I have tried many different ways of doing this based on similar issues from stackoverflow, but I am getting an error every time. This is my base code that still produces the error.
Class file with the Tkinter window
class GuardianLocator:
def __init__(self, master):
frame = Frame(master)
frame.grid()
master.title("GUARDIAN LOCATOR")
self.locator_label = Label(frame, text="Which Sailor Guardian are you looking for?", width=40, height=2)
self.locator_label.grid()
self.entry = Entry(frame)
self.entry.grid()
self.button1 = Button(frame, text="Search", command=self.guardian_name, pady=2)
self.button1.grid()
def guardian_name(self):
print(self.entry.get())
and in my main working file
root = Tk()
locator = guardian_locator.GuardianLocator(root)
root.mainloop()
This is my test loop to see if it's working.
if locator.guardian_input() is "Sailor Moon":
print("hi")
else:
print("no")
Not sure exactly how your code is organized and where is your "test loop" located, but I assume it is after root.mainloop(). Thus the script can be as follows:
from tkinter import *
class GuardianLocator:
def __init__(self, master):
self._name = ""
frame = Frame(master)
frame.grid()
master.title("GUARDIAN LOCATOR")
self.locator_label = Label(frame, text="Which Sailor Guardian are you looking for?", width=40, height=2)
self.locator_label.grid()
self.entry = Entry(frame)
self.entry.grid()
self.button1 = Button(frame, text="Search", command=self.guardian_name, pady=2)
self.button1.grid()
def guardian_name(self):
self._name = self.entry.get()
print(self.entry.get())
root = Tk()
locator = GuardianLocator(root)
root.mainloop()
# this will be executed after the root window is closed.
print("Name is", locator._name)
Please note self._name = "" in the constructor. This instance variable can be used to store the name provided in your GuardianLocator window.

Get the same input multiple times in python tkinter?

I am making a program to get multiple user names and store them in a XML file. The XML part works fine, but I am having trouble with the GUI. I want to be able to ask the user how many users he wants to input, then repeat an Entry that number of times. How can I do that, while maintaining a quit function? I tried:
def quitter():
exit()
quit()
quitterButton = Button(master, text="Quit", command=quitter)
mainCanvas.create_window(50, 330, window=quitterButton, tag="quitter")
num = int(raw_input("How many users are you going to put in?"))
for x in range(0,num):
#Create User entry Variable
userEnter = StringVar()
usrVar = ""
#Create enter box:
userEntryBox = Entry(master, textvariable = userEnter)
mainCanvas.create_window(250, 300, window=userEntryBox, tag="UserEnterA")
def gotInput(self):
usrVar = userEnter.get();
mainCanvas.create_text(250, 330, text="User Inputted: " + usrVar, tags="0")
mainCanvas.delete("UserEnterA")
#Bind entry box
userEntryBox.bind('<Key-Return>', gotInput)
userEntryBox.wait_window(userEntryBox)
#Create a new user element
newUsr= ET.Element('Member')
#Add element to the Root
root.append(newUsr)
#Make a sub element Name, set name
usrName = ET.SubElement(newUsr, 'Name')
usrName.text = usrVar;
...
tree.write('./output.xml')
What is the best way to go around it? I won't know the number of inputs, and I want the quit button to work at all times .
Behavior of your program is a bit unclear for me, but I try to help.
First solution: show tkinter askstring dialog num times. You can break for loop if user press Cancel button. It's not exactly what you want, but it's very easy to implement:
from tkinter import *
import tkinter.simpledialog as simpledialog
def add_users():
n = simpledialog.askinteger('', 'How many users are you going to put in?', initialvalue=1, minvalue=1, maxvalue=10)
if not n: # 'Cancel'
return
for i in range(n):
user = simpledialog.askstring('', 'User #%s from #%s' % (i+1, n))
if user is None: # 'Cancel'
return
# Do something useful
print(user)
root = Tk()
Button(root, text='Add users', command=add_users).pack(padx=50, pady=50)
Button(root, text='Quit', command=root.destroy).pack(pady=30)
root.mainloop()
Second (if you want to put entry and all new names to window with quit button):
from tkinter import *
import tkinter.simpledialog as simpledialog
class YourApp():
def __init__(self):
self.root = Tk()
Button(self.root, text='Quit', command=self.root.destroy).pack(pady=20)
self.ask_button = Button(self.root, text='Add users', command=self.add_users)
self.ask_button.pack(padx=50, pady=50)
self.root.mainloop()
def add_users(self):
self.users_count = 0
self.user_name = StringVar()
self.frame = Frame(self.root)
self.frame.pack()
self.users_count = simpledialog.askinteger('', 'How many users are you going to put in?', initialvalue=1, minvalue=1, maxvalue=10)
self.user_entry = Entry(self.frame, textvariable=self.user_name)
self.user_entry.pack(pady=10)
self.user_entry.bind('<Key-Return>', self.on_new_user)
self.user_entry.focus_set()
def on_new_user(self, event):
# your code
print(self.user_name.get())
Label(self.frame, text='User Inputted: %s' % self.user_name.get()).pack()
self.users_count -= 1
if not self.users_count:
self.user_entry.destroy()
self.user_name.set('')
YourApp()
There are three geometry managers in Tkinter: place (it's very similar to canvas in your case), grid and pack.
Canvas usually used for drawing pictures or graphs.

How to wait for return to be clicked with the entry widget?

I am just trying to make a non-graphic game in Tkinter but am having trouble with the entry widget. How can I wait for "<Return>" to be pressed before printing something?
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
f = Frame(master, width=500, height=300)
f.pack(fill=X, expand=True)
mt = StringVar()
menubar = Menu(master)
menubar.add_command(label="New Game", command=self.new_game)
menubar.add_command(label="Continue Game", command=self.continue_game)
master.config(menu=menubar)
maintext = Label(master, fg="blue", textvariable=mt)
maintext.pack(side=BOTTOM)
mt.set("")
self.e1 = Entry(master)
self.e1.pack()
self.e1.bind("<Return>")
self.e1.lower()
global mt
def new_game(self):
mt.set("What do you want your username to be?")
self.e1.lift()
#wait for <Return> to be pressed
mt.set("Welcome " + self.e1.get())
def continue_game(self):
mt.set("Type your username in.")
root = Tk()
app = App(root)
root.mainloop()
I want it to be when I click "New Game" on the top menubar, it shows the entry box and waits for me to type something in, and then click enter. THEN it prints out "Welcome"+(what the person types in). What actually happens is when I click New game, it immediately just prints "Welcome".
By print, I mean aet the Label at the bottom to something else, which is reffered to by "mt.set".
In GUI programs you don't wait* for something to happen, you respond to events. So, to call a function when the user presses return you would do something like:
self.e1.bind("<Return>", self._on_return)
The above will call the function _on_return when the user presses the return key.
In your specific code you don't really need a StringVar. For example, you could do this:
def __init__(self, master):
...
self.maintext = Label(master, fg="blue")
...
def _on_return(self, event):
self.maintext.configure(text="Welcome, %s" % self.e1.get())
* strictly speaking, you can wait for things, but that's not the right way to write GUI programs except under specific circumstances, such as waiting for a dialog to be dismissed.

Categories

Resources