python对话框代码_Python、tkinter、复杂对话框和代码结构
當實現復雜的對話框(即,具有大約10個或更多窗口小部件的對話框,尤其是在多個框架中排列時),創建需要許多tkinter調用,當代碼保持在單個方法中時,代碼可能會變得越來越復雜(難以讀取和維護)。一般來說,短函數/方法通常比長函數/方法更可取。在
我目前限制方法長度的方法是將屬于對話框中某個組的所有小部件的創建封裝到一個method(parent_frame, other_options)中,該程序返回如下頂層小部件:import tkinter as tk
class Dialog:
def __init__(self, master):
self.__master = master
self.create_gui(master)
def create_gui(self, frame, title = None):
if title: frame.title(title)
group_a = self.create_group_a(frame)
group_a.grid(row=0, column=0, sticky="nsew")
group_b = self.create_group_b(frame)
group_b.grid(row=1, column=0, sticky="nsew")
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
frame.rowconfigure(1, weight=1)
def create_group_a(self, frame):
inner_frame = tk.LabelFrame(frame, text="Label")
text = self.create_text_with_scrollbar(inner_frame)
text.pack(fill="both")
return inner_frame
def create_group_b(self, frame):
button = tk.Button(frame, text="Button")
return button
def create_text_with_scrollbar(self, frame):
text_frame = tk.Frame(frame)
text_frame.grid_rowconfigure(0, weight=1)
text_frame.grid_columnconfigure(0, weight=1)
text = tk.Text(text_frame)
text.grid(row=0, column=0, sticky="nsew")
scrollbar = tk.Scrollbar(text_frame, command=text.yview)
scrollbar.grid(row=0, column=1, sticky="nsew")
text['yscrollcommand'] = scrollbar.set
return text_frame
if __name__ == "__main__":
master = tk.Tk()
Dialog(master)
tk.mainloop()
在這種情況下,是否有關于代碼結構的具體指導方針?有人對如何更好地構建這樣的代碼有什么建議嗎?在
總結
以上是生活随笔為你收集整理的python对话框代码_Python、tkinter、复杂对话框和代码结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: obj: object是什么意思_面试官
- 下一篇: canopy算法流程_求助,kmeans