簡體   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()

所以基本上我想通過單擊按鈕打開一個新的 window 其代碼在另一個 class 中。 此按鈕在 class1 中。 我怎樣才能完成這個? function 是“open_main_window”,按鈕是“sharingan”。 我只想在單擊此按鈕時打開一個新的 window 我應該在 function 中寫什么代碼?

要打開一個新的 window 你使用tk.Toplevel()所以你的 function 將是:

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

有關tk.Toplevel()的更多信息,請查看本教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM