New window label - python

how do I position my label which says "Question One" in my def new_window() function. As you run it the label is being positioned at the bottom, And i want it to be applied on the top.
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=150, height=40)
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display.pack()
message.pack()
display2 = Label(root, width=100, height=30, bg='green')
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label1 = Label(text="A Quiz Game", font = ("Arial", "24"), padx=40,
pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
display2.pack()
button1.pack()
message_label1.pack()
message_label2.pack()
root.mainloop() # Runs the main window loop

You are packing in the wrong order. Do not pack display before your message. So just swapping the order will fix the issue.
Here is the code. Replace your def new_window(): with this
def new_window():
newWindow = Toplevel()
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display = Label(newWindow, width=150, height=40)
message.pack()
display.pack()
pack method just blindly packs the widget into the window. And the next pack will be done below it if there is space. So take care of the order while packing widgets :)

Related

How do I make my button know what`s in the entry box and then print that value in a new window?

I want after you write something in the entry box and then you press the button a new window to pop up and
the number that was written in the entry box to be printed in the new window as " Your height is: "value" but after many tries I still don`t understand how to do it.
my code:
import tkinter as tk
root = tk.Tk()
root.geometry("250x130")
root.resizable(False, False)
lbl = tk.Label(root, text="Input your height", font="Segoe, 11").place(x=8, y=52)
entry = tk.Entry(root,width=15).place(x=130, y=55)
btn1 = tk.Button(root, text="Enter", width=12, height=1).place(x=130, y=85) #command=entrytxt1
root.mainloop()
This is what I got:
import tkinter as tk
root = tk.Tk()
root.resizable(False, False)
def callback():
# Create a new window
new_window = tk.Toplevel()
new_window.resizable(False, False)
# `entry.get()` gets the user input
new_window_lbl = tk.Label(new_window, text="You chose: "+entry.get())
new_window_lbl.pack()
# `new_window.destroy` destroys the new window
new_window_btn = tk.Button(new_window, text="Close", command=new_window.destroy)
new_window_btn.pack()
lbl = tk.Label(root, text="Input your height", font="Segoe, 11")
lbl.grid(row=1, column=1)
entry = tk.Entry(root, width=15)
entry.grid(row=1, column=2)
btn1 = tk.Button(root, text="Enter", command=callback)
btn1.grid(row=1, column=3)
root.mainloop()
Basically when the button is clicked it calls the function named callback. It creates a new window, gets the user's input (entry.get()) and puts it in a label.

How to get a value entered in Top level widget in tkinter and use it in main window to display?

I'm new to Python GUI and trying to learn using tkinter, I have created a main window on clicking the button "Continue" in the main window, a top level window opens and a number can be entered in the entry box, I'm not able to read the value in main window. Can anybody help me with how to read the value entered in the top level window and enter the same in the main window
from tkinter import *
# global variable
blank_2 = []
blank_1 = []
# Function to create a top level window with entry boxes
def open_new_window():
# Toplevel object which will be treated as a new window
popup = Toplevel(main)
global blank_2
global blank_1
# sets the title of the Toplevel widget
popup.title("Enter the value")
# sets the geometry of toplevel
popup.geometry("480x220")
popup.geometry("+500+250")
popup.configure(background='grey')
PL = Label(popup, text="Enter the 1st value :", fg="white", bg="grey",
font=('century gothic', 12))
PL.place(x=20, y=20)
GW = Label(popup, text="Enter the 2nd value :", fg="white", bg="grey",
font=('century gothic', 12))
GW.place(x=20, y=70)
blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_2.place(x=210, y=20)
blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_1.place(x=210, y=70)
exitp = Button(popup, text='Quit', font=8, width=5, height=1, bd=4, command=popup.destroy)
setp = Button(popup, text='Set', font=8, bd=4, command=sum)
exitp.place(x=300, y=120)
setp.place(x=240, y=120)
blank_2.focus()
# Function to insert the value from the top level window to main window
def sum():
global blank_2, blank_1
LS.insert(END, blank_2)
LS.insert(END, blank_1)
main = Tk()
main.title("TEST") # required text on window title
main.configure(background='grey') # Background color of Main window
main.geometry("+600+150") # Position of the window on the screen
main.geometry("400x450") # Size of the window
# text window to display the value in main window
LS = Text(main, bd=3, height=3, width=30, bg="light cyan")
LS.place(x=20, y=190)
# buttons to open a top level window and to quit the main window
C = Button(main, text='Continue', font=8, bd=4, command=open_new_window)
C.place(x=40, y=45)
Q = Button(main, text='Quit', font=8, width=5, height=1, bd=4, command=main.destroy)
Q.place(x=150, y=45)
mainloop()
The output in the main window
You need to call the get method of the Entry´s like
LS.insert(END, blank_2.get())
LS.insert(END, blank_1.get())
since you defined blank_1 and blank_2 as Entry´s:
blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
.!toplevel.!entry
is the reference for tkinter to the wdiget.

How to add multiline text dynamically in tkinter python?

I have created a chat application, in which i use ListBox for showing the chat history. It looks good until I enter a long sting which goes beyond the screen. Is there a way to break the string and show in new line or any other way to show the complete string. I'm new to Tkinter and im not aware of many widgets available.
Here is my sample code
from tkinter import *
class Actions:
def chatUpdate(chat):
chatlist.insert(Actions.chatLast,chat)
Actions.chatLast=Actions.chatLast+1
chatlist.pack( side=LEFT, fill=BOTH)
chatBox.config(command=chatlist.yview)
def callUpdater():
txt=textBox.get()
text_text.set("")
Actions.chatUpdate(txt)
root = Tk()
root.title("Chat App")
frame1 = Frame(root, bd=4)
frame1.pack(side=TOP)
frame2 = Frame(root, bd=4)
frame2.pack(side=TOP)
frame3 = Frame(root, bd=4)
frame3.pack(side=TOP)
# chat box
chatBox = Scrollbar(frame1)
chatBox.pack(side=RIGHT, fill=Y)
chatlist = Listbox(frame1, yscrollcommand = chatBox.set, width=50)
Actions.chatLast=0
Actions.chatUpdate(" ")
# text box
textView = Label(frame2, text="Input: ")
textView.pack(side=LEFT)
text_text = StringVar()
textBox = Entry(frame2, textvariable=text_text, bd=0, width=40, bg="pink")
textBox.pack(side=RIGHT)
# send button
button = Button(frame3, text="Send", fg="black", command=callUpdater)
button.pack(side=TOP)
root.mainloop()
You can replace the Listbox by a Text widget in 'disabled' mode which automatically wraps long lines. You will just need to put the widget back in 'normal' mode each time you insert text:
from tkinter import *
def callUpdater():
text = textBox.get()
textBox.delete(0, 'end')
chat.configure(state='normal')
chat.insert('end', text + '\n')
chat.configure(state='disabled')
root = Tk()
chatBox = Scrollbar(root)
chat = Text(root, wrap='word', state='disabled', width=50,
yscrollcommand=chatBox.set)
chatBox.configure(command=chat.yview)
chat.grid(row=0, columnspan=2, sticky='ewns')
chatBox.grid(row=0, column=2, sticky='ns')
Label(root, text="Input: ").grid(row=1, column=0)
textBox = Entry(root, bd=0, width=40, bg="pink")
textBox.grid(row=1, column=1)
Button(root, text="Send", command=callUpdater).grid(row=2, columnspan=2)
root.mainloop()
By the way, both the Listbox and Text widgets support the index 'end' so you don't have to keep track of how many lines you have inserted.

How to enhance quiz game for multiple choice?

I am not sure how I should create this as a "multiple choice" quiz game.
I don't know how to continue on. I am not sure how to use class in this instance. If I can get the first question working with answers, I'm sure I am continue on from there but I am in need of help.
Here is my current code:
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel()
message = Label(newWindow, text="What is the capital of France?", font =
("Arial", "24"), pady=30)
display = Label(newWindow, width=150, height=40)
class letsQuiz:
def __init__(self, questions, answers):
self.questions = questions
self.answers = answers
self.label = Label(newWindow, text="What is the capital of France?")
self.label.pack()
self.answer1button = Button(newWindow, text="Madrid")
self.answer2button = Button(newWindow, text="Paris")
self.answer3button = button(newWindow, text="London")
self.answer1button.pack()
self.answer2button.pack()
self.answer3button.pack()
message.pack()
display.pack()
message_label1 = Label(text="Let's quiz some of your knowledge :)", font = (
"Arial", "25"), padx=40, pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
display2 = Label(root, width=100, height=30)
message_label1.pack()
message_label2.pack()
button1.pack()
display2.pack()
root.mainloop() # Runs the main window loop
There you go, This should set you up with a good base to work on. I've added few comments to help you out. I've kept most your code intact so you can understand easily. Note that this is no where an optimal way to code a good quiz game. But okay if you're starting on it.
Scoring can be done using lot of if statements and instance variables to keep track of. Good luck !
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel()
message = Label(newWindow, font =
("Arial", "24"), pady=30)
display = Label(newWindow, width=150, height=40)
return newWindow
class letsQuiz:
def __init__(self, window):
self.newWindow = window
self.question_counter = 0
# Add your questions and answers here
self.questions = ['# QUESTION: 1', '# QUESTION: 2', '# QUESTION: 3', '# QUESTION: 4']
# Each list is a set of answers corresponding to question
self.answers = [['question_1_Answer_1', 'question_1_Answer_2', 'question_1_Answer_3'],
['question_2_Answer_1', 'question_2_Answer_2', 'question_2_Answer_3'],
['question_3_Answer_1', 'question_3_Answer_2', 'question_3_Answer_3'],
['question_4_Answer_1', 'question_4_Answer_2', 'question_4_Answer_3']]
self.label = Label(self.newWindow, text=self.questions[self.question_counter], font =
("Arial", "24"), pady=30)
self.answer1button = Button(self.newWindow, text=self.answers[self.question_counter][0])
self.answer2button = Button(self.newWindow, text=self.answers[self.question_counter][1])
self.answer3button = Button(self.newWindow, text=self.answers[self.question_counter][2])
self.nextButton = Button(self.newWindow, text="Next", command=self.next_question)
def pack_all(self):
'''
Packs all widgets into the window
'''
self.label.pack()
self.answer1button.pack()
self.answer2button.pack()
self.answer3button.pack()
self.nextButton.pack()
def forget_all(self):
'''
Removes all widgets from the window
'''
self.label.pack_forget()
self.answer1button.pack_forget()
self.answer2button.pack_forget()
self.answer3button.pack_forget()
self.nextButton.pack_forget()
def next_question(self):
'''
Updates the question and its corresponding answers
'''
self.question_counter += 1
self.forget_all() # remove old question
try:
self.label = Label(self.newWindow, text=self.questions[self.question_counter], font =
("Arial", "24"), pady=30)
self.answer1button = Button(self.newWindow, text=self.answers[self.question_counter][0])
self.answer2button = Button(self.newWindow, text=self.answers[self.question_counter][1])
self.answer3button = Button(self.newWindow, text=self.answers[self.question_counter][2])
self.nextButton = Button(self.newWindow, text="Next", command=self.next_question)
except IndexError:
self.forget_all()
msg = Label(self.newWindow, text="You have completed the quiz Thank you for playing, Close to EXIT", font =
("Arial", "24"), pady=30)
msg.pack()
self.pack_all() # place in the new question
quiz = letsQuiz(new_window() )
#message.pack()
#display.pack()
message_label1 = Label(text="Let's quiz some of your knowledge :)", font = (
"Arial", "25"), padx=40, pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
button1 = Button(root, text ="Continue", command=quiz.pack_all, width=16,
bg="red")
display2 = Label(root, width=100, height=30)
message_label1.pack()
message_label2.pack()
button1.pack()
display2.pack()
root.mainloop() # Runs the main window loop

Tkinter new window

I'm relatively new to Tkinter and I need help.
I have created a new window when a button is clicked from the parent window. The new window is the def new_Window. But I can't seem to get the information in the window as coded below:
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() #Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=200, height=50)
message = Label(root, text="Welcome")
display.pack()
message.pack()
display2 = Label(root, width=100, height=30)
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
username = StringVar() #Stores the username in text
user_entry = Entry(root, textvariable=username) #Creates an entry for the
username
user_entry.pack()
display2.pack()
button1.pack()
message_label.pack()
root.mainloop()#Runs the main window loop
Thanks for your help.
You did not pack the hello label into the new window. A tip is also to use background colors to visualize labels when developing. Here is a functioning code for you. I only changed 2 lines, added foreground and background.
from tkinter import *
from tkinter import ttk
# User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=200, height=50,bg='RED')
message = Label(newWindow, text="HEEEY",fg='BLACK',bg='GREEN')
message.pack()
display.pack()
display2 = Label(root, width=100, height=30)
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
username = StringVar() # Stores the username in text
user_entry = Entry(root, textvariable=username) # Creates an entry for the
username
user_entry.pack()
display2.pack()
button1.pack()
message_label.pack()
root.mainloop() # Runs the main window loop

Categories