动态调整大小并将小部件添加到 Tkinter 窗口 [英] Dynamically resizing and adding widgets to Tkinter window

查看:39
本文介绍了动态调整大小并将小部件添加到 Tkinter 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python Tkinter GUI,它从用户那里请求文件名.选择每个文件时,我想在窗口的其他位置添加一个 Entry() 框 - 是否可以在 Tkinter 中执行此操作?

I have a Python Tkinter GUI which solicits file names from the user. I would like to add an Entry() box elsewhere in the window when each file is selected -- is it possible to do this in Tkinter?

谢谢
标记

推荐答案

是的,这是可能的.你这样做就像你添加任何其他小部件一样——调用 Entry(...) 然后使用它的 gridpackplace 方法让它在视觉上显示出来.

Yes, it is possible. You do it like you add any other widget -- call Entry(...) and then use its grid, pack or place method to have it show up visually.

这是一个人为的例子:

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()

这篇关于动态调整大小并将小部件添加到 Tkinter 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆