在 python 中使用 Tkinter 编辑标题栏

我试图添加一个自定义标题的窗口,但我有麻烦与它。我知道我的代码不正确,但是当我运行它时,它创建了2个窗口,一个只有 title tk,另一个更大的窗口有“ Simple Prog”。如何使 tk 窗口的标题为“ Simple Prog”而不是新的附加窗口。我不认为我应该有 Tk ()部分,因为当我在我的完整代码,有一个错误

from tkinter import Tk, Button, Frame, Entry, END


class ABC(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.parent = parent
self.pack()
ABC.make_widgets(self)


def make_widgets(self):
self.root = Tk()
self.root.title("Simple Prog")
258872 次浏览

self.parent is a reference to the actual window, so self.root.title should be self.parent.title, and self.root shouldn't exist.

If you don't create a root window, Tkinter will create one for you when you try to create any other widget. Thus, in your __init__, because you haven't yet created a root window when you initialize the frame, Tkinter will create one for you. Then, you call make_widgets which creates a second root window. That is why you are seeing two windows.

A well-written Tkinter program should always explicitly create a root window before creating any other widgets.

When you modify your code to explicitly create the root window, you'll end up with one window with the expected title.

Example:

from tkinter import Tk, Button, Frame, Entry, END


class ABC(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.parent = parent
self.pack()
self.make_widgets()


def make_widgets(self):
# don't assume that self.parent is a root window.
# instead, call `winfo_toplevel to get the root window
self.winfo_toplevel().title("Simple Prog")


# this adds something to the frame, otherwise the default
# size of the window will be very small
label = Entry(self)
label.pack(side="top", fill="x")


root = Tk()
abc = ABC(root)
root.mainloop()

Also note the use of self.make_widgets() rather than ABC.make_widgets(self). While both end up doing the same thing, the former is the proper way to call the function.

Try something like:

from tkinter import Tk, Button, Frame, Entry, END


class ABC(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()


root = Tk()
app = ABC(master=root)
app.master.title("Simple Prog")
app.mainloop()
root.destroy()

Now you should have a frame with a title, then afterwards you can add windows for different widgets if you like.

Having just done this myself you can do it this way:

from tkinter import Tk, Button, Frame, Entry, END


class ABC(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.parent = parent
self.pack()
ABC.make_widgets(self)


def make_widgets(self):
self.parent.title("Simple Prog")

You will see the title change, and you won't get two windows. I've left my parent as master as in the Tkinter reference stuff in the python library documentation.

For anybody who runs into the issue of having two windows open and runs across this question, here is how I stumbled upon a solution:

The reason the code in this question is producing two windows is because

Frame.__init__(self, parent)

is being run before

self.root = Tk()

The simple fix is to run Tk() before running Frame.__init__():

self.root = Tk()
Frame.__init__(self, parent)

Why that is the case, I'm not entirely sure.

Here it is nice and simple.

root = tkinter.Tk()
root.title('My Title')

root is the window you create and root.title() sets the title of that window.

widget.winfo_toplevel().title("My_Title")

changes the title of either Tk or Toplevel instance that the widget is a child of.

Example of python GUI


Here is an example:

from tkinter import *;
screen = Tk();
screen.geometry("370x420"); //size of screen

Change the name of window

  screen.title('Title Name')

Run it:

screen.mainloop();

I found this works:

window = Tk()
window.title('Window')

Maybe this helps?

Easy method:

root = Tk()
root.title('Hello World')

One point that must be stressed out is: The .title() method must go before the .mainloop()

Example:


from tkinter import *


# Instantiating/Creating the object
main_menu = Tk()


# Set title
main_menu.title("Hello World")


# Infinite loop
main_menu.mainloop()


Otherwise, this error might occur:

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 2217, in wm_title
return self.tk.call('wm', 'title', self._w, string)
_tkinter.TclError: can't invoke "wm" command: application has been destroyed

And the title won't show up on the top frame.

I found a solution that should help you:

from tkinter import Tk, Button, Frame, Entry, END


class ABC(Frame):
def __init__(self,master=None):
super().__init__(master)
self.pack()
self.master.title("Simple Prog")
self.make_widgets()


def make_widgets(self):
pass


root = Tk()
app = ABC(master=root)
app.mainloop()

Found at: docs.python.org