Python 更改 Tkinter 框架标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33637292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Tkinter Frame Title
提问by user3798654
I am trying to figure out how to change the title of a Tkinter Frame. Below is simplified code that mimics the portion of my program where I am trying to change the title:
我想弄清楚如何更改 Tkinter 框架的标题。下面是模仿我试图更改标题的程序部分的简化代码:
from Tkinter import *
class start_window(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
Frame.pack(self)
Label(self, text = 'Test', width=30).pack()
if __name__ == '__main__':
start_window().mainloop()
With this sample code the Frame has the standard "tk" title but I would like to change it to something like "My Database". I've tried everything I can think of with no success. Any help would be appreciated.
使用此示例代码,框架具有标准的“tk”标题,但我想将其更改为“我的数据库”之类的内容。我已经尝试了所有我能想到的方法,但都没有成功。任何帮助,将不胜感激。
采纳答案by letsc
Try this:
尝试这个:
if __name__ == '__main__':
root = Tk()
root.title("My Database")
root.geometry("500x400")
app = start_window(root)
root.mainloop()
回答by Bryan Oakley
First, you should be explicitly creating the main window by creating an instance of Tk
. When you do, you can use the reference to this window to change the title.
首先,您应该通过创建Tk
. 当您这样做时,您可以使用对此窗口的引用来更改标题。
I also recommend notusing a global import. Instead, import tkinter by name,and prefix your tkinter commands with the module name. I use the name tk
to cut down on typing:
我还建议不要使用全局导入。相反,按名称导入 tkinter,并使用模块名称作为 tkinter 命令的前缀。我用这个名字tk
来减少打字:
import Tkinter as tk
class start_window(tk.Frame):
def __init__(self, parent=None):
tk.Frame.__init__(self, parent)
tk.Frame.pack(self)
tk.Label(self, text = 'Test', width=30).pack()
if __name__ == '__main__':
root = tk.Tk()
root.wm_title("This is my title")
start_window(root)
root.mainloop()
Finally, to make your code easier to read I suggest giving your class name an uppercase first letter to be consistent with almost all python programmers everywhere:
最后,为了让你的代码更容易阅读,我建议你给你的类名一个大写的第一个字母,以与几乎所有地方的所有 python 程序员保持一致:
class StartWindow(...):
By using the same conventions as everyone else, it makes it easier for us to understand your code.
通过使用与其他人相同的约定,我们可以更轻松地理解您的代码。
For more information about naming conventions used by the tkinter community, see PEP8
回答by Athina
I generally start my tkinter apps with
我通常开始我的 tkinter 应用程序
#!/usr/local/bin/python3
import Tkinter as tk
root = Tk()
root.title('The name of my app')
root.minsize(300,300)
root.geometry("800x800")
root.mainloop()