简体   繁体   English

在tkinter中从其他窗口打开窗口

[英]Opening windows from other windows in tkinter

Here is my code: 这是我的代码:

#!/usr/bin/python

from Tkinter import *

def openWindowReasons():                        
    global win 

    win = Tk()                    

    win.title("Decem Rationes Computerum Programmandorum")

    buttonMaker("I", 1, 1, " I) Programmo computeres ne meus animus deceat ignavus." )                            #create buttons 1-10 
    buttonMaker("II", 2, 1, " II) Programmo computeres ut relaxem post scholam et gaudeam scholam esse perfectam.")
    buttonMaker("III", 3, 1, " III) Meo amico programmato programmo ut certem cum eo.")
    buttonMaker("IV", 4, 1, " IV) Alii homines qui boni porgrammi programmant ut facian pecuniam.")
    buttonMaker("V", 5, 1, " V) Alii homines scribunt tantam codem ut alii cogitent eos esse optimos.")
    buttonMaker("VI", 1, 3, " VI) Programmare est tam iocus ut sperem me programmaturum esse saepius.")
    buttonMaker("VII", 2, 3, " VII) Multi homines programmant tam ut non habent tempus faciendorum pensorum quod sunt quam insulso.")
    buttonMaker("VIII", 3, 3, " VIII) Programmo ut possim loqui de eo cum meis amicis.")
    buttonMaker("IX", 4, 3, " IX) Me inspirato ab caeteris programmo ut sim similaris eis.")
    buttonMaker("X", 5, 3, " X) Saepe programmo ne habeam audire mea matri quae dictit quam multa.")

    label_1 = Label(win, text="RATIONES")        
    label_1.grid(row=3, column=2)



def buttonMaker(a, b, c, d):
    f = "button" + a
    f= Button(win, text=a, commmand=openWindowR(str(d)))
    f.grid(row=int(b), column=int(c))


def openWindowR(d):
    newWin = Tk()

    newWin.title(str(d))

    g = "label" + d 

    g = Label(newWin, text=str(d)) 
    g.grid(row=3, column=3)


def openWindowAlt():
    newWin_1 = Toplevel()
    label_2 = Label(newWin_1, text="Magister Bartoloma tam sapiens ut Minerva, quae dea sapientae est, eum admiretur.")


root = Tk()            #create parent window

root.title("Main Page")

label = Label(root, text="Decem Rationes Computerum Programmandorum")    
label.grid(row=1, column=2)

button = Button(root, text="Decem Rationes", commmand=openWindowReasons())  
button.grid(row=3, column=3)

button_1= Button(root, text="Pleasant Alternative", commmand=openWindowAlt())  
button_1.grid(row=3, column=1)

root.mainloop()

Every time I run the code it opens all the windows at once instead of when the user presses the buttons...any help would be greatly appreciated... 每次我运行代码时,它都会立即打开所有窗口,而不是在用户按下按钮时打开。所有帮助将不胜感激。

It is because you are calling the functions, instead of passing the reference, or using a lambda function. 这是因为您在调用函数,而不是传递引用或使用lambda函数。

f = Button(win, text=a, command=lambda d=d: openWindowR(str(d)))

However, this is only a walkaround. 但是,这只是一个解决方法。 You should consider using classes in your program instead of global variables and functions (making your code much cleaner and organized!). 您应该考虑在程序中使用类,而不是全局变量和函数(使您的代码更加简洁和井井有条!)。

Apart from this suggestion, you are creating two Tk instances. 除此建议外,您还要创建两个Tk实例。 If you need to create a new window, use a Toplevel widget, but Tkinter programs should have only one Tk element. 如果需要创建一个新窗口,请使用“顶层”窗口小部件,但Tkinter程序应仅包含一个Tk元素。 If not, it can lead you to unexpected problems. 如果没有,可能会导致您遇到意想不到的问题。

This is exactly the same implementation using a class, called Application , where you have all the functionality of your GUI. 这与使用名为Application的类的实现完全相同,该类具有GUI的所有功能。

#!/usr/bin/python

from Tkinter import *


class Application:
    def __init__(self, master):
        self.master = master
        self.master.title("Main Page")
        self.label = Label(master, text="Decem Rationes Computerum Programmandorum")
        self.label.grid(row=1, column=2)
        self.button = Button(master, text="Decem Rationes", command=self.openWindowReasons)  
        self.button.grid(row=3, column=3)
        self.button_1= Button(master, text="Pleasant Alternative", command=self.openWindowAlt)  
        self.button_1.grid(row=3, column=1)

    def openWindowAlt(self):
        newWin_1 = Toplevel()
        label_2 = Label(newWin_1, text="Magister Bartoloma tam sapiens ut Minerva, quae dea sapientae est, eum admiretur.")
        label_2.pack()

    def openWindowReasons(self):                        
        self.toplevel = Toplevel()
        self.buttonMaker("I", 1, 1, " I) Programmo computeres ne meus animus deceat ignavus." )
        self.buttonMaker("II", 2, 1, " II) Programmo computeres ut relaxem post scholam et gaudeam scholam esse perfectam.")
        self.buttonMaker("III", 3, 1, " III) Meo amico programmato programmo ut certem cum eo.")
        self.buttonMaker("IV", 4, 1, " IV) Alii homines qui boni porgrammi programmant ut facian pecuniam.")
        self.buttonMaker("V", 5, 1, " V) Alii homines scribunt tantam codem ut alii cogitent eos esse optimos.")
        self.buttonMaker("VI", 1, 3, " VI) Programmare est tam iocus ut sperem me programmaturum esse saepius.")
        self.buttonMaker("VII", 2, 3, " VII) Multi homines programmant tam ut non habent tempus faciendorum pensorum quod sunt quam insulso.")
        self.buttonMaker("VIII", 3, 3, " VIII) Programmo ut possim loqui de eo cum meis amicis.")
        self.buttonMaker("IX", 4, 3, " IX) Me inspirato ab caeteris programmo ut sim similaris eis.")
        self.buttonMaker("X", 5, 3, " X) Saepe programmo ne habeam audire mea matri quae dictit quam multa.")
        label_1 = Label(self.toplevel, text="RATIONES")        
        label_1.grid(row=3, column=2)

    def buttonMaker(self, a, b, c, d):
        f = Button(self.toplevel, text="button" + a, command=lambda d=d: self.openWindowR(str(d)))
        f.grid(row=int(b), column=int(c))

    def openWindowR(self, d):
        newWin = Toplevel()
        newWin.title(str(d))
        g = Label(newWin, text="label" + d) 
        g.grid()


root = Tk()
app = Application(root)
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM