简体   繁体   English

如何在 tkinter 的另一个 class 中打开一个新的 window

[英]How to open a new window which is in another class in tkinter

import tkinter as tk
from tkinter import *
import PIL
from PIL import Image
from PIL import ImageTk, Image
import time


class Sharingan(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("60x60+1465+750")
        self.config()

        self.photo_sharingan = ImageTk.PhotoImage(file="sharingan.png")

#this button has to open a new window on button click:     
        self.btn_sharingan = Button(image=self.photo_sharingan, bd=0,
                                    command=lambda: [self.change_btn_img(),self.open_main_window()])
        self.btn_sharingan.place(x=-3, y=-4)

        self.photo_sharingan2 = ImageTk.PhotoImage(file="sharingan-2.png")
        self.btn_sharingan2 = Button(image=self.photo_sharingan2, bd=0, command=lambda: [self.change_btn_img2()],
                                     highlightbackground="black")
        self.btn_sharingan2.place_forget()

        self.overrideredirect(True)

    def change_btn_img(self):
        self.btn_sharingan.place_forget()
        self.btn_sharingan2.place(x=-3, y=-4)

    def change_btn_img2(self):
        self.btn_sharingan2.place_forget()
        self.btn_sharingan.place(x=-3, y=-4)

#this is the function but how do I make it to initialize and mainloop class-MainWindow
def open_main_window():
    

class MainWindow(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("200x100")



app1 = Sharingan()
app1.mainloop()

So basically I want to open a new window whose code is in another class from a button click.所以基本上我想通过单击按钮打开一个新的 window 其代码在另一个 class 中。 This button is in class1.此按钮在 class1 中。 How can I accomplsh this?我怎样才能完成这个? The function is 'open_main_window' an the button is 'sharingan'. function 是“open_main_window”,按钮是“sharingan”。 I just want to open a new window when this button is clicked what code should I write in the function?我只想在单击此按钮时打开一个新的 window 我应该在 function 中写什么代码?

To open a new window you use tk.Toplevel() so your function would be:要打开一个新的 window 你使用tk.Toplevel()所以你的 function 将是:

def open_main_window(self):
   self.newWindow = tk.Toplevel()

For more info on tk.Toplevel() check out this tutorial有关tk.Toplevel()的更多信息,请查看本教程

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

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