import tkinter as tk from tkinter import filedialog def browse_file1(): file_path = filedialog.askopenfilename() print("Selected file:", file_path) file_path_text.set(file_path) def browse_file2(): file_path = filedialog.askopenfilename() print("Selected file:", file_path) file_path_text.set(file_path) def browse_file3(): 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_file1) browse_button.pack(padx=10, pady=10) file_path_text = tk.StringVar() browse_button = tk.Button(frame, text="上传", command=browse_file2) browse_button.pack(padx=10, pady=10) file_path_text = tk.StringVar() browse_button = tk.Button(frame, text="上传", command=browse_file3) 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="") 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="") label2.pack(pady=10) text2 = tk.StringVar() entry2 = tk.Entry(frame, textvariable=text2, width=50) entry2.pack(padx=10, pady=10) root.mainloop()修改成三个文本框放左边右边对应着三个上传接口 每个文本框上面放标题名
时间: 2024-03-16 10:46:28 浏览: 64
Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法详解
这段代码可以通过将三个上传按钮和对应的文本框分别放置在左右两个Frame中来实现三个文本框放左边右边对应着三个上传接口,每个文本框上面放标题名的效果。以下是修改后的代码:
```
import tkinter as tk
from tkinter import filedialog
def browse_file1():
file_path = filedialog.askopenfilename()
print("Selected file:", file_path)
file_path_text1.set(file_path)
def browse_file2():
file_path = filedialog.askopenfilename()
print("Selected file:", file_path)
file_path_text2.set(file_path)
def browse_file3():
file_path = filedialog.askopenfilename()
print("Selected file:", file_path)
file_path_text3.set(file_path)
width = int(800)
height = int(350)
width_offset = 400
height_offset = 250
root = tk.Tk()
root.title("文件浏览器")
# 左边的Frame
left_frame = tk.Frame(root, width=400, height=350)
left_frame.pack(side=tk.LEFT, padx=20, pady=20)
# 右边的Frame
right_frame = tk.Frame(root, width=400, height=350)
right_frame.pack(side=tk.RIGHT, padx=20, pady=20)
# 左边的第一个文件上传按钮和文本框
browse_button1 = tk.Button(left_frame, text="上传", command=browse_file1)
browse_button1.pack(padx=10, pady=10)
file_path_text1 = tk.StringVar()
file_path_entry1 = tk.Entry(left_frame, textvariable=file_path_text1, width=30)
file_path_entry1.pack(padx=10, pady=10)
label1 = tk.Label(left_frame, text="文件1")
label1.pack(pady=10)
# 左边的第二个文件上传按钮和文本框
browse_button2 = tk.Button(left_frame, text="上传", command=browse_file2)
browse_button2.pack(padx=10, pady=10)
file_path_text2 = tk.StringVar()
file_path_entry2 = tk.Entry(left_frame, textvariable=file_path_text2, width=30)
file_path_entry2.pack(padx=10, pady=10)
label2 = tk.Label(left_frame, text="文件2")
label2.pack(pady=10)
# 左边的第三个文件上传按钮和文本框
browse_button3 = tk.Button(left_frame, text="上传", command=browse_file3)
browse_button3.pack(padx=10, pady=10)
file_path_text3 = tk.StringVar()
file_path_entry3 = tk.Entry(left_frame, textvariable=file_path_text3, width=30)
file_path_entry3.pack(padx=10, pady=10)
label3 = tk.Label(left_frame, text="文件3")
label3.pack(pady=10)
# 右边的第一个文本框和标题
text1 = tk.StringVar()
entry1 = tk.Entry(right_frame, textvariable=text1, width=50)
entry1.pack(padx=10, pady=10)
title1 = tk.Label(right_frame, text="标题1")
title1.pack(pady=10)
# 右边的第二个文本框和标题
text2 = tk.StringVar()
entry2 = tk.Entry(right_frame, textvariable=text2, width=50)
entry2.pack(padx=10, pady=10)
title2 = tk.Label(right_frame, text="标题2")
title2.pack(pady=10)
# 右边的第三个文本框和标题
text3 = tk.StringVar()
entry3 = tk.Entry(right_frame, textvariable=text3, width=50)
entry3.pack(padx=10, pady=10)
title3 = tk.Label(right_frame, text="标题3")
title3.pack(pady=10)
root.geometry('{}x{}+{}+{}'.format(width, height, width_offset, height_offset))
root.mainloop()
```
这样就可以在界面中看到三个文本框放在左边,对应着三个上传接口,每个文本框上方有一个标题。右边放置了三个文本框,每个文本框上方也有一个标题。
阅读全文