簡體   English   中英

動態調整大小並將小部件添加到Tkinter窗口

[英]Dynamically resizing and adding widgets to Tkinter window

我有一個Python Tkinter GUI,可以從用戶那里獲取文件名。 選擇每個文件時,我想在窗口的其他位置添加一個Entry()框-是否可以在Tkinter中做到這一點?

謝謝
標記

對的,這是可能的。 您可以像添加其他任何小部件一樣進行操作-調用Entry(...) ,然后使用其gridpackplace方法使其以視覺方式顯示。

這是一個人為的示例:

import Tkinter as tk
import tkFileDialog

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.button = tk.Button(text="Pick a file!", command=self.pick_file)
        self.button.pack()
        self.entry_frame = tk.Frame(self)
        self.entry_frame.pack(side="top", fill="both", expand=True)
        self.entry_frame.grid_columnconfigure(0, weight=1)

    def pick_file(self):
        file = tkFileDialog.askopenfile(title="pick a file!")
        if file is not None:
            entry = tk.Entry(self)
            entry.insert(0, file.name)
            entry.grid(in_=self.entry_frame, sticky="ew")
            self.button.configure(text="Pick another file!")

app = SampleApp()
app.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM