如何在tkinter python3中通过/不通过面向对象的方式链接两个窗口?

2024-04-20 10:53:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用PyCharm作为IDE学习Python3.x中tinker的基础知识。我正在构建一个基本的两窗口程序,在该程序中,单击欢迎窗口中的CustomerButton后应调用“Customer choice”窗口。 欢迎窗口有两个按钮-客户和员工。 客户选择窗口有两个按钮-订单和取消订单。 出于某种原因,会出现一个空白的“客户选择”窗口,而不是预期的窗口

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.

Tags: imagecoordinatepngplacebuttonrootwindowwidth
1条回答
网友
1楼 · 发布于 2024-04-20 10:53:26

将“root”传递给customerwindow函数,并使用顶层而不是第二个Tk()实例,这可能会导致问题。当您发布无法运行的代码(例如使用图像等)时,不要期望将来有太多帮助

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")

相关问题 更多 >