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()
所以基本上我想通過單擊按鈕打開一個新視窗,其代碼位于另一個類中。此按鈕在 class1 中。我怎樣才能做到這一點?函式是“open_main_window”,按鈕是“sharingan”。我只想在單擊此按鈕時打開一個新視窗,我應該在函式中撰寫什么代碼?
uj5u.com熱心網友回復:
要打開您使用的新視窗tk.Toplevel(),您的功能將是:
def open_main_window(self):
self.newWindow = tk.Toplevel()
有關tk.Toplevel()查看本教程的更多資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383488.html
下一篇:在不同的列中顯示元素
