Python Tkinter列表生成器函数自动运行、ubuntu主题不生效问题求助
Python列表生成器代码问题修复
问题1:Donn函数自动执行,初始显示空列表[]
- 触发原因:按钮
command参数传入带括号的Donn(),代码运行到按钮定义行时会直接执行该函数,不会等到点击触发。 - 修复方案:将
command = Donn()修改为command = Donn,仅传入函数引用。
问题2:ubuntu主题未生效
- 触发原因:ttkthemes的主题仅对ttk系列控件生效,代码中使用的
Label、Button、Entry均为原生tkinter控件,无法适配自定义主题。 - 修复方案:将所有控件声明替换为ttk前缀控件,如
Label改为ttk.Label、Button改为ttk.Button、Entry改为ttk.Entry。
修正后完整代码
from tkinter import * from tkinter import ttk import tkinter.font as TkFont import tkinter from ttkthemes import themed_tk as tk import random root = tk.ThemedTk() root.get_themes() root.set_theme("ubuntu") root.title(" List Generator") root.geometry("300x300") root.resizable(0 , 0) Head = ttk.Label (root , text = "List Generator for Python" , font=("Arial Bold" , 15)) Head.pack(anchor = CENTER ) lbl1 = ttk.Label (root , text = '''Type Words to make a List Use space to separate Words in List''' ) lbl1.pack(anchor = CENTER) ListBox = ttk.Entry( root , width = 40) ListBox.place( x = 26 , y = 100) lbl2 = ttk.Label( root , text= "") lbl2.place( x = 26 , y = 175) def Donn(): Done = ListBox.get() Output = Done.split() lbl2.config(text = str(Output) ) Btn_Done = ttk.Button( root , text = "Convert" , command = Donn ) Btn_Done.place(x = 117 , y = 125 ) root.mainloop()
内容的提问来源于stack exchange,提问作者1254 HARSSHAVARDHAN K
相关产品推荐
相关产品推荐

