简体   繁体   English

无法更改 tkinter 中的程序标题

[英]cant change title of program in tkinter

this is my main module(this is for a point of sale system)这是我的主要模块(这是用于销售点系统)

from tkinter import *
from SettingsMenuPOS import *
from Globalvariables import *



    root = Tk() #mainprogram
     
    root.iconbitmap('D:/Gatlabs logo.ico')
    
    
    
    opensettingsmenu = Button(root, text= "Open Settings", command = settingsmain)
    root.title(Mname)
    
    
    
    opensettingsmenu.grid(row= 0, column= 0)
    
        
    enter_button = Button( root , text = "ENTER", padx = 20, pady = 10, command= EnterEvent)
     
    enter_button.grid(row= 0, column= 1)
    
    root.mainloop()

im importing a setting module which changes the title of the program我正在导入一个更改程序标题的设置模块

from Globalvariables import *
from tkinter import *

#Mart Name
#user accounts




def settingsmain():

 settingmenu = Toplevel()
 settingmenu.iconbitmap('D:/Gatlabs logo.ico')

 global entryformartname

 labelformartname = Label(settingmenu, text = "Enter name of your store")
 entryformartname = Entry(settingmenu)
 entryformartname.grid(row = 0, column = 0)

 setmartname = Button(settingmenu, text = "setname", command = setname)
 setmartname.grid(row= 0, column = 1)

 settingmenu.mainloop()


def setname():

    global Mname, entryformartname
    Mname = entryformartname.get()

and im using a global variable Mname to display title of the program.我使用全局变量 Mname 来显示程序的标题。 ive set the variable Mname = "Gatlabs" and the user can change it through entry.我设置了变量 Mname = "Gatlabs" 并且用户可以通过输入来更改它。 but everytime i try to set the title it doesnt change:( i hope to get better at coding but i suck. pls help im stuck但是每次我尝试设置标题时它都不会改变:(我希望在编码方面做得更好,但我很烂。请帮助我卡住

I think the problem here is that you are thinking that using -:我认为这里的问题是您正在考虑使用-:

root.title(var)

would make it so that anytime you change the var the title will change, but actually its that the title is changed only once and to the value of the var at the time it was supplied.将使得任何时候您更改 var 标题都会更改,但实际上标题仅更改一次,并且更改为 var 在提供时的值。

So if you want it to change everytime you change your var, then rather put it in the same function as the one where you change your var.因此,如果您希望每次更改 var 时都更改它,那么请将其放在与更改 var 相同的 function 中。

def update_title(title) :
     global var
     
     var = title
     root.title(title)
     return

and now every time you change the title run this down with the desired title as the argument.现在每次更改标题时,都会以所需的标题作为参数运行它。

I hope this solves your problem.我希望这能解决你的问题。 Also i hope you're safe in the time of an ongoing pandemic.我也希望你在持续的大流行期间是安全的。

Change setname function like this:像这样更改setname function:

def setname():
    global Mname, entryformartname, setmartname 
    Mname = entryformartname.get()
    
    parent_name = setmartname.winfo_parent()
    parent = setmartname._nametowidget(parent_name)
    parent.title(Mname)

In your code, you just update Mname variable and window title stays unchanged.在您的代码中,您只需更新Mname变量,并且 window 标题保持不变。

Update:更新:

You can get the master from button widget which is the window itself.您可以从 window 本身的按钮小部件中获取主控件。 Then, change its title.然后,更改其标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM