import tkinter as tk from tkinter import filedialog def browse_file(): file_path = filedialog.askopenfilename() print("Selected file:", file_path) file_path_text.set(file_path) width = int(450) height = int(285) width_offset = 550 #offset 指定当点画模式时填充位置的偏移,参数值为 "x,y"height_offset = 250 root = tk.Tk() root.title("文件浏览器") frame = tk.Frame(root) frame.pack(padx=20, pady=20) root.geometry('{}x{}+{}+{}'.format(width, height, width_offset, height_offset)) browse_button = tk.Button(frame, text="浏览文件", command=browse_file) browse_button.pack(padx=10, pady=10) file_path_text = tk.StringVar() file_path_entry = tk.Entry(frame, textvariable=file_path_text, width=50) file_path_entry.pack(padx=10, pady=10) root.mainloop()给这个代码添加几个文本框
时间: 2024-03-11 19:47:02 浏览: 160
好的,可以通过以下代码在文件浏览器中添加两个文本框:
```python
import tkinter as tk
from tkinter import filedialog
def browse_file():
file_path = filedialog.askopenfilename()
print("Selected file:", file_path)
file_path_text.set(file_path)
width = int(450)
height = int(350) # 增加了高度以容纳两个文本框
width_offset = 550
height_offset = 250
root = tk.Tk()
root.title("文件浏览器")
frame = tk.Frame(root)
frame.pack(padx=20, pady=20)
root.geometry('{}x{}+{}+{}'.format(width, height, width_offset, height_offset))
browse_button = tk.Button(frame, text="浏览文件", command=browse_file)
browse_button.pack(padx=10, pady=10)
file_path_text = tk.StringVar()
file_path_entry = tk.Entry(frame, textvariable=file_path_text, width=50)
file_path_entry.pack(padx=10, pady=10)
# 添加第一个文本框
label1 = tk.Label(frame, text="文本框1:")
label1.pack(pady=10)
text1 = tk.StringVar()
entry1 = tk.Entry(frame, textvariable=text1, width=50)
entry1.pack(padx=10, pady=10)
# 添加第二个文本框
label2 = tk.Label(frame, text="文本框2:")
label2.pack(pady=10)
text2 = tk.StringVar()
entry2 = tk.Entry(frame, textvariable=text2, width=50)
entry2.pack(padx=10, pady=10)
root.mainloop()
```
这样就可以在文件浏览器中添加两个文本框了。
阅读全文
相关推荐


















