python使用tkinter可以在多个操作系统_如何在Python中同时运行多个tkinter窗口?

本文介绍了一个使用Python的Tkinter库创建独立窗口的例子。通过继承Toplevel类而非Thread类,可以实现在同一应用中打开独立的窗口。示例代码展示了如何创建带有滚动条和输入框的笔记窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tkinter中的顶层不是子类化Thread,而是子类Toplevel,它是同一应用程序中的一个单独的窗口,听起来就像您正试图完成的:from tkinter import *

#from threading import Thread #no longer needed

class Note(Toplevel):

nid = 0

#title = "" #this would block the method to override the current title

message = ""

def __init__(self, master, nid, title, message):

Toplevel.__init__(self,master)

self.nid = nid

self.title(title) #since toplevel widgets define a method called title you can't store it as an attribute

self.message = message

self.display_note_gui() #maybe just leave that code part of the __init__?

def display_note_gui(self):

'''Tkinter to create a note gui window with parameters '''

#no window, just self

self.geometry("200x200")

self.configure(background="#BAD0EF")

#pass self as the parent to all the child widgets instead of window

title = Entry(self,relief=FLAT, bg="#BAD0EF", bd=0)

title.pack(side=TOP)

scrollBar = Scrollbar(self, takefocus=0, width=20)

textArea = Text(self, height=4, width=1000, bg="#BAD0EF", font=("Times", "14"))

scrollBar.pack(side=RIGHT, fill=Y)

textArea.pack(side=LEFT, fill=Y)

scrollBar.config(command=textArea.yview)

textArea.config(yscrollcommand=scrollBar.set)

textArea.insert(END, self.message)

#self.mainloop() #leave this to the root window

def run(self):

self.display_note_gui()

root = Tk()

root.withdraw() #hide the root so that only the notes will be visible

new_note1 = Note(root, 0, "Hello", "Hi, how are you?")

#new_note1.start()

#new_note1.join()

new_note2 = Note(root, 1, "2", "How's everyone else?")

#new_note2.start()

#new_note2.join()

root.mainloop() #still call mainloop on the root

请注意,您可以调用self.title()来获取窗口的当前标题,并调用{}来更改它,而不是将标题存储为属性。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值