upload_button.grid(row=0, column=0, sticky=tkinter.EW)
时间: 2023-10-03 08:05:37 浏览: 89
这是一个使用 tkinter 模块创建 GUI 界面时,将上传按钮放在第一行第一列,并且在水平方向上占据整个单元格的代码。其中,grid() 方法用于将组件放置在网格布局中,row 和 column 分别指定组件所在的行数和列数,sticky 参数则用于控制组件在单元格中的对齐方式。在本例中,sticky=tkinter.EW 表示将组件水平方向上拉伸到单元格的左右两边。
相关问题
import tkinter as tk from tkinter import ttk from forex_python.converter import CurrencyRates # 创建货币转换器对象 c = CurrencyRates() # 创建主窗口 root = tk.Tk() root.title('货币转换器') # 创建控件 amount_label = ttk.Label(root, text='金额:') amount_entry = ttk.Entry(root) from_currency_label = ttk.Label(root, text='从货币类型:') from_currency_combo = ttk.Combobox(root, values=['USD', 'EUR', 'JPY', 'GBP']) to_currency_label = ttk.Label(root, text='到货币类型:') to_currency_combo = ttk.Combobox(root, values=['USD', 'EUR', 'JPY', 'GBP']) convert_button = ttk.Button(root, text='转换') result_label = ttk.Label(root, text='转换结果:') result_value = ttk.Label(root, text='') # 控件布局 amount_label.grid(row=0, column=0, padx=5, pady=5, sticky='w') amount_entry.grid(row=0, column=1, padx=5, pady=5) from_currency_label.grid(row=1, column=0, padx=5, pady=5, sticky='w') from_currency_combo.grid(row=1, column=1, padx=5, pady=5) to_currency_label.grid(row=2, column=0, padx=5, pady=5, sticky='w') to_currency_combo.grid(row=2, column=1, padx=5, pady=5) convert_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5) result_label.grid(row=4, column=0, padx=5, pady=5, sticky='w') result_value.grid(row=4, column=1, padx=5, pady=5) # 定义转换函数 def convert(): amount = float(amount_entry.get()) from_currency = from_currency_combo.get() to_currency = to_currency_combo.get() result = c.convert(from_currency, to_currency, amount) result_value.config(text=result) # 绑定事件 convert_button.config(command=convert) # 运行主循环 tk.mainloop() 优化此程序使窗口能运行
以下是优化后的代码:
```python
import tkinter as tk
from tkinter import ttk
from forex_python.converter import CurrencyRates
class CurrencyConverter:
def __init__(self, root):
self.root = root
self.root.title('货币转换器')
self.c = CurrencyRates()
self.create_widgets()
def create_widgets(self):
# 创建控件
amount_label = ttk.Label(self.root, text='金额:')
self.amount_entry = ttk.Entry(self.root)
from_currency_label = ttk.Label(self.root, text='从货币类型:')
self.from_currency_combo = ttk.Combobox(self.root, values=['USD', 'EUR', 'JPY', 'GBP'])
to_currency_label = ttk.Label(self.root, text='到货币类型:')
self.to_currency_combo = ttk.Combobox(self.root, values=['USD', 'EUR', 'JPY', 'GBP'])
convert_button = ttk.Button(self.root, text='转换', command=self.convert)
result_label = ttk.Label(self.root, text='转换结果:')
self.result_value = ttk.Label(self.root, text='')
# 控件布局
amount_label.grid(row=0, column=0, padx=5, pady=5, sticky='w')
self.amount_entry.grid(row=0, column=1, padx=5, pady=5)
from_currency_label.grid(row=1, column=0, padx=5, pady=5, sticky='w')
self.from_currency_combo.grid(row=1, column=1, padx=5, pady=5)
to_currency_label.grid(row=2, column=0, padx=5, pady=5, sticky='w')
self.to_currency_combo.grid(row=2, column=1, padx=5, pady=5)
convert_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
result_label.grid(row=4, column=0, padx=5, pady=5, sticky='w')
self.result_value.grid(row=4, column=1, padx=5, pady=5)
def convert(self):
amount = float(self.amount_entry.get())
from_currency = self.from_currency_combo.get()
to_currency = self.to_currency_combo.get()
result = self.c.convert(from_currency, to_currency, amount)
self.result_value.config(text=result)
if __name__ == '__main__':
# 创建主窗口并运行
root = tk.Tk()
currency_converter = CurrencyConverter(root)
root.mainloop()
```
这里将原来的代码封装在一个类 `CurrencyConverter` 中,使得代码更加模块化和易读。将 `convert()` 方法绑定到转换按钮上,实现转换功能。同时将控件中需要动态修改的部分(如输入框、下拉框、结果标签)设为实例属性,方便在 `convert()` 方法中调用和修改。最后在 `__main__` 中创建主窗口并运行。
import os import tkinter as tk from tkinter import filedialog, messagebox from pdfminer.high_level import extract_text root = tk.Tk() root.title("PDF文档信息搜索") def browse_file(): file_path = filedialog.askopenfilename() if file_path: file_entry.delete(0, tk.END) file_entry.insert(0, file_path) def search(): file_path = file_entry.get() keyword = keyword_entry.get() if not os.path.exists(file_path): messagebox.showerror("错误", "文件不存在") return try: text = extract_text(file_path) if keyword in text: result_text.delete("1.0", tk.END) result_text.insert(tk.END, text) else: messagebox.showinfo("提示", "未找到关键词") except: messagebox.showerror("错误", "提取文本信息时出错") file_label = tk.Label(root, text="选择文件:") file_label.grid(row=0, column=0, padx=5, pady=5, sticky="e") file_entry = tk.Entry(root, width=50) file_entry.grid(row=0, column=1, padx=5, pady=5) file_button = tk.Button(root, text="浏览", command=browse_file) file_button.grid(row=0, column=2, padx=5, pady=5) keyword_label = tk.Label(root, text="搜索关键词:") keyword_label.grid(row=1, column=0, padx=5, pady=5, sticky="e") keyword_entry = tk.Entry(root, width=50) keyword_entry.grid(row=1, column=1, padx=5, pady=5) search_button = tk.Button(root, text="搜索", command=search) search_button.grid(row=2, column=1, padx=5, pady=5) result_label = tk.Label(root, text="搜索结果:") result_label.grid(row=3, column=0, padx=5, pady=5, sticky="e") result_text = tk.Text(root, width=80, height=20) result_text.grid(row=4, column=0, columnspan=3, padx=5, pady=5) root.mainloop()上述代码添加一个保存搜索结果按钮
可以在GUI界面中添加一个保存搜索结果的按钮,并在按钮的回调函数中添加文件保存的逻辑。下面是添加保存按钮的代码:
```python
def save_result():
result = result_text.get("1.0", tk.END)
if not result:
messagebox.showinfo("提示", "搜索结果为空,无法保存")
return
file_path = file_entry.get()
file_dir, file_name = os.path.split(file_path)
output_file = os.path.join(file_dir, "search_result.txt")
with open(output_file, "w", encoding="utf-8") as f:
f.write(result)
messagebox.showinfo("提示", f"搜索结果已保存到 {output_file}")
save_button = tk.Button(root, text="保存结果", command=save_result)
save_button.grid(row=2, column=2, padx=5, pady=5)
```
在这个代码中,我们定义了一个`save_result`函数,用于保存搜索结果。函数中首先获取搜索结果的文本内容,并检查是否为空。如果结果不为空,则获取输入的文件路径,并从中提取出文件夹路径和文件名,然后将搜索结果保存到同级目录下名为`search_result.txt`的文件中。最后使用`messagebox`弹出一个提示框,告知保存结果的位置。
然后在主函数中添加保存按钮的代码,即可实现保存搜索结果的功能:
```python
search_button = tk.Button(root, text="搜索", command=search)
search_button.grid(row=2, column=1, padx=5, pady=5)
save_button = tk.Button(root, text="保存结果", command=save_result)
save_button.grid(row=2, column=2, padx=5, pady=5)
result_label = tk.Label(root, text="搜索结果:")
result_label.grid(row=3, column=0, padx=5, pady=5, sticky="e")
result_text = tk.Text(root, width=80, height=20)
result_text.grid(row=4, column=0, columnspan=3, padx=5, pady=5)
```
完整代码如下:
```python
import os
import tkinter as tk
from tkinter import filedialog, messagebox
from pdfminer.high_level import extract_text
root = tk.Tk()
root.title("PDF文档信息搜索")
def browse_file():
file_path = filedialog.askopenfilename()
if file_path:
file_entry.delete(0, tk.END)
file_entry.insert(0, file_path)
def search():
file_path = file_entry.get()
keyword = keyword_entry.get()
if not os.path.exists(file_path):
messagebox.showerror("错误", "文件不存在")
return
try:
text = extract_text(file_path)
if keyword in text:
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, text)
else:
messagebox.showinfo("提示", "未找到关键词")
except:
messagebox.showerror("错误", "提取文本信息时出错")
def save_result():
result = result_text.get("1.0", tk.END)
if not result:
messagebox.showinfo("提示", "搜索结果为空,无法保存")
return
file_path = file_entry.get()
file_dir, file_name = os.path.split(file_path)
output_file = os.path.join(file_dir, "search_result.txt")
with open(output_file, "w", encoding="utf-8") as f:
f.write(result)
messagebox.showinfo("提示", f"搜索结果已保存到 {output_file}")
file_label = tk.Label(root, text="选择文件:")
file_label.grid(row=0, column=0, padx=5, pady=5, sticky="e")
file_entry = tk.Entry(root, width=50)
file_entry.grid(row=0, column=1, padx=5, pady=5)
file_button = tk.Button(root, text="浏览", command=browse_file)
file_button.grid(row=0, column=2, padx=5, pady=5)
keyword_label = tk.Label(root, text="搜索关键词:")
keyword_label.grid(row=1, column=0, padx=5, pady=5, sticky="e")
keyword_entry = tk.Entry(root, width=50)
keyword_entry.grid(row=1, column=1, padx=5, pady=5)
search_button = tk.Button(root, text="搜索", command=search)
search_button.grid(row=2, column=1, padx=5, pady=5)
save_button = tk.Button(root, text="保存结果", command=save_result)
save_button.grid(row=2, column=2, padx=5, pady=5)
result_label = tk.Label(root, text="搜索结果:")
result_label.grid(row=3, column=0, padx=5, pady=5, sticky="e")
result_text = tk.Text(root, width=80, height=20)
result_text.grid(row=4, column=0, columnspan=3, padx=5, pady=5)
root.mainloop()
```
阅读全文