1

I've started learning Tkinter on Python few weeks ago and wanted to create a Guess Game. But unfortunately I ran into a problem, with this code the text for the rules ( in the function Rules; text='Here are the rules... ) doesn't appear on the window ( rule_window).


window = Tk()
window.title("Guessing Game")

welcome = Label(window,text="Welcome To The Guessing Game!",background="black",foreground="white")
welcome.grid(row=0,column=0,columnspan=3)

def Rules():
   rule_window = Tk()
   rule_window = rule_window.title("The Rules")
   the_rules = Label(rule_window, text='Here are the rules...', foreground="black")
   the_rules.grid(row=0,column=0,columnspan=3)
   rule_window.mainloop()

rules = Button(window,text="Rules",command=Rules)
rules.grid(row=1,column=0,columnspan=1)

window.mainloop() 

Does anyone know how to solve this problem?

Flusten
  • 56
  • 1
  • 5

2 Answers2

3

In your code you reset whatever rule_window is to a string (in this line: rule_window = rule_window.title(...))

Try this:

from import tkinter *

window = Tk()
window.title("Guessing Game")

welcome = Label(window, text="Welcome To The Guessing Game!", background="black", foreground="white")
welcome.grid(row=0, column=0, columnspan=3)

def Rules():
   rule_window = Toplevel(window)
   rule_window.title("The Rules")
   the_rules = Label(rule_window, text="Here are the rules...", foreground="black")
   the_rules.grid(row=0, column=0, columnspan=3)

rules = Button(window, text="Rules", command=Rules)
rules.grid(row=1, column=0, columnspan=1)

window.mainloop()

When you want to have 2 windows that are responsive at the same time you can use tkinter.Toplevel().

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
TheLizzard
  • 7,248
  • 2
  • 11
  • 31
0

In your code, you have initialized the new instances of the Tkinter frame so, instead of you can create a top-level Window. What TopLevel Window does, generally creates a popup window kind of thing in the application. You can also trigger the Tkinter button to open the new window.

from tkinter import *
from tkinter import ttk

#Create an instance of tkinter window
root= Tk()
root.geometry("600x450")

#Define a function
def open_new():
    #Create a TopLevel window
    new_win= Toplevel(root)
    new_win.title("My New Window")

    #Set the geometry
    new_win.geometry("600x250")
    Label(new_win, text="Hello There!",font=('Georgia 15 bold')).pack(pady=30)

#Create a Button in main Window
btn= ttk.Button(root, text="New Window",command=open_new)
btn.pack()

root.mainloop()