-1

I am learning basics of tinker in python 3.x using PyCharm as an IDE.I am building a basic two window program where 'Customer choice' window should be called after clicking CustomerButton in Welcome Window. Welcome window has two buttons- customer and staff. customer choice window has two buttons- order and cancel order. For some reason, a blank 'Customer choice' window appears instead of the intended one.

from tkinter import *

print("aa")

def customerwindow():
    root1 = Tk()
    root1.title("Customer Choice")

    window_width = 1000
    window_height = 600

    screen_height = root1.winfo_screenheight()
    screen_width = root1.winfo_screenwidth()

    x_coordinate = (screen_width / 2) - (window_width / 2)
    y_coordinate = (screen_height / 2) - (window_height / 2)

    root1.geometry('%dx%d+%d+%d' % (window_width, window_height, x_coordinate, y_coordinate))

    frame1 = Frame(root1, height=1000, width=600)
    frame1.pack(fill=BOTH)

    Order = PhotoImage(file="fast-food.png")
    Cancel = PhotoImage(file="delete.png")

    OrderButton = Button(frame1, image=Order)
    OrderButton.place(x=152, y=125)
    CancelButton = Button(frame1, image=Cancel)
    CancelButton.place(x=550, y=125)

    OrderLabel = Label(frame1, text="       ORDER", font=("Poiret One", 24), fg='#34495E')
    CancelLabel = Label(frame1, text=" CANCEL ORDER", font=("Poiret One", 24), fg='#34495E')

    OrderLabel.place(x=152, y=405)
    CancelLabel.place(x=550, y=405)

    mainloop()

print("bb")

def welcomeWindow():
    root = Tk()
    root.title("Welcome")

    window_width = 1000
    window_height = 600

    screen_height = root.winfo_screenheight()
    screen_width = root.winfo_screenwidth()

    x_coordinate = (screen_width/2) - (window_width/2)
    y_coordinate = (screen_height/2) - (window_height/2)

    root.geometry('%dx%d+%d+%d' % (window_width, window_height, x_coordinate, y_coordinate))

    frame1 = Frame(root, height=1000, width=600)
    frame1.pack(fill=BOTH)

    customerPhoto = PhotoImage(file="boy.png")
    staffPhoto = PhotoImage(file="boss.png")

    customerButton = Button(frame1, image=customerPhoto, command=customerwindow)
    customerButton.place(x=152, y=125)
    staffButton = Button(frame1, image=staffPhoto)
    staffButton.place(x=550, y=125)

    customerLabel = Label(frame1, text="   CUSTOMER", font=("Poiret One", 24), fg='#34495E')
    staffLabel = Label(frame1, text="         STAFF", font=("Poiret One", 24), fg='#34495E')

    customerLabel.place(x=152, y=405)
    staffLabel.place(x=550, y=405)

    mainloop()


welcomeWindow()


print("SS")

#aa,bb,ss are just to check the interpreters progress.

1 Answers1

0

Pass "root" to the customerwindow function and use a Toplevel instead of a 2nd Tk() instance which can cause problems. Don't expect much help in the future when you post code that can't be run, i.e. uses images, etc.

import sys
if 3 == sys.version_info[0]: ## 3.X is default if dual system
    import tkinter as tk     ## Python 3.x
else:
    import Tkinter as tk     ## Python 2.x

from functools import partial

print("aa")

def customerwindow(root):
    root1 = tk.Toplevel(root)
    root1.title("Customer Choice")

    window_width = 1000
    window_height = 600

    screen_height = root1.winfo_screenheight()
    screen_width = root1.winfo_screenwidth()

    x_coordinate = (screen_width / 2) - (window_width / 2)
    y_coordinate = (screen_height / 2) - (window_height / 2)

    root1.geometry('%dx%d+%d+%d' % (window_width, window_height, x_coordinate, y_coordinate))

    frame1 = tk.Frame(root1, height=1000, width=600, bg="lightblue")
    frame1.pack(fill=tk.BOTH)

##    Order = PhotoImage(file="fast-food.png")
##    Cancel = PhotoImage(file="delete.png")

    """---- these buttons don't do anything
    OrderButton = Button(frame1, text="Order")
    OrderButton.place(x=152, y=125)
    """
    CancelButton = tk.Button(frame1, text="Cancel", command=root1.destroy)
    CancelButton.place(x=550, y=125)

    OrderLabel = tk.Label(frame1, text="       ORDER", font=("Poiret One", 24), fg='#34495E')
    CancelLabel = tk.Label(frame1, text=" CANCEL ORDER", font=("Poiret One", 24), fg='#34495E')

    OrderLabel.place(x=152, y=405)
    CancelLabel.place(x=550, y=405)

##  mainloop()

print("bb")

def welcomeWindow():
    root = tk.Tk()
    root.title("Welcome")

    window_width = 1000
    window_height = 600

    screen_height = root.winfo_screenheight()
    screen_width = root.winfo_screenwidth()

    x_coordinate = (screen_width/2) - (window_width/2)
    y_coordinate = (screen_height/2) - (window_height/2)

    root.geometry('%dx%d+%d+%d' % (window_width, window_height, x_coordinate, y_coordinate))

    frame1 = tk.Frame(root, height=1000, width=600)
    frame1.pack(fill=tk.BOTH)

##  customerPhoto = PhotoImage(file="boy.png")
##  staffPhoto = PhotoImage(file="boss.png")

    customerButton = tk.Button(frame1, text="customerPhoto", command=partial(customerwindow, root))
    customerButton.place(x=152, y=125)

    ## this button doesn't do anything
    ##staffButton = tk.Button(frame1, text="staffPhoto")
    ##staffButton.place(x=550, y=125)

    customerLabel = tk.Label(frame1, text="   CUSTOMER", font=("Poiret One", 24), fg='#34495E')
    staffLabel = tk.Label(frame1, text="         STAFF", font=("Poiret One", 24), fg='#34495E')

    customerLabel.place(x=152, y=405)
    staffLabel.place(x=550, y=405)

    root.mainloop()


welcomeWindow()


print("SS")