如何将 2 个不同的 python 3 tkinter GUI 程序与不同的几何管理器集成



我是python的新手。 我正在尝试使用 python3 中的 Tkinter 创建一个小型 GUI 应用程序。 我想要实现的功能是

1)程序必须创建一个小窗口,从用户那里接收搜索字符串。

在此处输入图像描述

2)一旦用户输入字符串并点击搜索按钮,程序必须从Excel表中检索数据并将结果显示在Tkinter表(熊猫表模块)中。

我已经为这两个功能分别编写了代码,但无法将它们放在一起

以下是实现功能 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 表。

我需要帮助将这两段代码集成到一个文件中。

  1. 用户输入搜索字符串并点击搜索
  2. 然后,检索到的数据应显示在表中。

我认为错误出在几何管理器上,因为我试图在单个实例中使用 pack() 和 grid() 类型。但我真的不知道如何把这两段代码没有任何冲突并实现功能。我也是 python 的 OOP 概念的新手。

提前谢谢。

关于第 1 点):TL;DR,为什么这么多代码。我没有通读它,但从用户那里读取一个字符串似乎很多。它还做其他事情吗?

为什么不直接使用简单的对话框?

message = simpledialog.askstring('Tri2Find','Enter text',parent=root)

或者,如果您需要自己的设计和/或验证,则可以从simpledialog继承构建自己的对话框。

最新更新