【发布时间】:2018-05-06 14:48:40
【问题描述】:
我是 python 新手。我正在尝试在 python3 中使用 Tkinter 创建一个小型 GUI 应用程序。我想要实现的功能是
1) 程序必须创建一个小窗口来接收用户的搜索字符串。
2) 一旦用户输入字符串并点击搜索按钮,程序必须从 excel 表中检索数据并将结果显示在 Tkinter 表(pandas 表模块)中。
我为这两个功能分别编写了代码,无法将它们放在一起
这里是实现功能1的代码。
from tkinter import *
from pandastable import Table, TableModel
import pandas as pd
# Instance of class tkinter Tk
root = Tk()
# Icon for the window
root.iconbitmap('D:/icon.ico')
# Window Title
root.title('Tri2Find')
df_in = pd.read_excel('D:/tmp/data.xlsx',index_col = None)
# Input variable for entry string
entry_search_string = StringVar()
# Text Field for search string
entry_search = Entry(root, width = 50,textvariable =
entry_search_string).grid(row = 0, column = 1)
# Function for different actions of [GO SEARCH!!] Button
def button_entry_search():
search_string = entry_search_string.get()
# Create a label when button is clicked and print the search term in the label
label_entry_search = Label(root, text = "SEARCHING : "+search_string ).grid(row = 0, column = 2)
# Creating a list for holding the index values
index = []
# Iterating over each row of the data frame
for i in range(len(df_in.index)):
# Converting each row of a data frame into a pandas series
row = pd.Series(df_in.iloc[i,:])
# Check for the user's search token in each row
pattern_boolean = row.str.contains(search_string, case = False, na = False)
# If presence of token is true
if pattern_boolean.any() == True:
# Then append the value of i to the index
index.append(i) # Index contains the row indicies with the required search term
# Data frame which contains the rows with required search term
df_out = df_in.iloc[index,:]
print(df_out)
# [GO SEARCH!!] Button of search term
button_search = Button(root,text = "GO SEARCH!!", width = 13,command =
button_entry_search).grid(row = 0)
# loop function to Run and keep the GUI open
root.mainloop()
上面的代码接受字符串并将结果打印到控制台但不打印到 tkinter 表
这是功能 2 的代码。
from tkinter import *
from pandastable import Table, TableModel
import pandas as pd
# Reading the excel from local path
df_in = pd.read_excel('D:/tmp/data.xlsx',index_col = None)
# Reading user input from console
search_token = input("Please Enter the search term :")
# Print a status message
print("Showing results for..." +str(search_token))
# Creating a list for holding the index values
index = []
# Iterating over each row of the data frame
for i in range(len(df_in.index)):
# Converting each row of a data frame into a pandas series
row = pd.Series(df_in.iloc[i,:])
# Check for the user's search token in each row
pattern_boolean = row.str.contains(search_token, case = False, na = False)
# If presence of token is true
if pattern_boolean.any() == True:
# Then append the value of i to the index
index.append(i) # Index contains the row indicies with the required search term
# Data frame which contains the rows with required search term
df_out = df_in.iloc[index,:]
class results_table(Frame):
def __init__(self, parent=None):
self.parent = parent
Frame.__init__(self)
self.main = self.master
self.main.geometry('600x400+200+100')
self.main.iconbitmap('D:/icon.ico')
self.main.title('Tri2Find')
f = Frame(self.main)
f.pack(fill=BOTH,expand=1)
self.table=Table(f, dataframe=df_out,showtoolbar=True, showstatusbar=True)
self.table.show()
return
app = results_table()
#launch the app
app.mainloop()
上述代码从控制台获取输入,但将输出提供给 Tkinter 表。
我需要帮助将这两段代码集成到一个文件中。
- 用户输入搜索字符串并点击搜索
- 然后检索到的数据应该显示在表中。
我认为错误出在几何管理器上,因为我试图在单个实例中使用 pack() 和 grid() 类型。但我真的不知道如何将这 2 段代码没有任何冲突并实现功能。我也是 Python 的 OOP 概念的新手。
提前致谢。
【问题讨论】:
标签: python-3.x user-interface tkinter grid pack