最近在使用组件Lable时,遇见了text内容不能动态修改,最后发现是自己写错了。
动态修改现在发现有两种方法,一种是修改Lable的text属性的办法,一种是用varibale的双向绑定,
首页是text属性的办法,请看代码
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/10/2 上午1:05 # @Author : wangying # @Site : # @File : TkinterFTP同步界面.py # @Software: PyCharm #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/10/2 上午12:41 # @Author : wangying # @Site : # @File : FTP选择文件框.py # @Software: PyCharm from tkinter import * # 导入ttk from tkinter import ttk # 导入filedialog from tkinter import filedialog class App: def __init__(self, master): self.master = master self.is_open_type = False #False表示当前未开启,True表示当前开启 self.initWidgets() def initWidgets(self): print(self.str.get()) self.lable_title = ttk.Label(self.master,text= '麦嘎FTP自动同步文件',background = "white",font=("微软雅黑", 20)) self.lable_time = ttk.Label(self.master, text=self.str,font=("微软雅黑", 28),foreground='blue',background='red') self.button_sync = ttk.Button(self.master, text='开启同步', command=self.sync_file) self.button_down_files = ttk.Button(self.master, text='选择本地下载目录', command=self.open_directory) self.button_jason_file = ttk.Button(self.master, text='Jason配置文件', command=self.open_jason_file) self.lable_title.grid(row=1, column=1,columnspan=2,pady=10) self.lable_time.grid(row=2,column=1,columnspan=2) self.button_sync.grid(row=3,column=1,columnspan=2,pady=10) self.button_down_files.grid(row=4,column=1,padx=35,pady=20) self.button_jason_file.grid(row=4, column=2,padx=35,pady=20) def sync_file(self): if self.is_open_type == False: self.button_sync['text'] = '关闭' self.lable_time['text'] = '修改的内容为关闭' self.is_open_type = True else: self.button_sync['text'] = '开启同步' self.lable_time['text'] = '修改的内容为开启' self.is_open_type = False print(self.lable_title['text']) def open_directory(self): # 调用askdirectory方法打开目录 print(filedialog.askdirectory(title='打开目录',initialdir='d:/')) # 初始目录 def open_jason_file(self): pass root = Tk() root.title('集团FTP自动下载文件') root.geometry("400x200+200+200") App(root) root.mainloop()就是通过Lable['text'] 来获取和修改属性值
第二种通过variable来实现双向绑定,
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/10/2 上午1:05 # @Author : wangying # @Site : # @File : TkinterFTP同步界面.py # @Software: PyCharm #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/10/2 上午12:41 # @Author : wangying # @Site : # @File : FTP选择文件框.py # @Software: PyCharm from tkinter import * # 导入ttk from tkinter import ttk # 导入filedialog from tkinter import filedialog class App: def __init__(self, master): self.master = master self.is_open_type = False #False表示当前未开启,True表示当前开启 self.str = StringVar() self.str.set('0天 00:00:00') self.initWidgets() def initWidgets(self): print(self.str.get()) self.lable_title = ttk.Label(self.master,text= '麦嘎FTP自动同步文件',background = "white",font=("微软雅黑", 20)) self.lable_time = ttk.Label(self.master, textvariable=self.str,font=("微软雅黑", 28),foreground='blue',background='red') self.button_sync = ttk.Button(self.master, text='开启同步', command=self.sync_file) self.button_down_files = ttk.Button(self.master, text='选择本地下载目录', command=self.open_directory) self.button_jason_file = ttk.Button(self.master, text='Jason配置文件', command=self.open_jason_file) self.lable_title.grid(row=1, column=1,columnspan=2,pady=10) self.lable_time.grid(row=2,column=1,columnspan=2) self.button_sync.grid(row=3,column=1,columnspan=2,pady=10) self.button_down_files.grid(row=4,column=1,padx=35,pady=20) self.button_jason_file.grid(row=4, column=2,padx=35,pady=20) def sync_file(self): if self.is_open_type == False: self.button_sync['text'] = '关闭' self.str.set('修改的内容为关闭') self.is_open_type = True else: self.button_sync['text'] = '开启同步' self.str.set('修改的内容为开启') self.is_open_type = False print(self.lable_title['text']) def open_directory(self): # 调用askdirectory方法打开目录 print(filedialog.askdirectory(title='打开目录',initialdir='d:/')) # 初始目录 def open_jason_file(self): pass root = Tk() root.title('集团FTP自动下载文件') root.geometry("400x200+200+200") App(root) root.mainloop() 对Lable属性不用text而要使用textvariable,对值设置使用.set()方法,获取值用.get()方法。