import tkinter as tk
import tkinter.ttk as ttk
import sys
import tkinter.messagebox as tkm
import pandas as pd
import openpyxl
from tkinter.scrolledtext import ScrolledText
import numpy as np
import tkinter.simpledialog as simpledialog
import webbrowser
import pprint
from urlextract import URLExtract
import warnings
warnings.simplefilter('ignore', FutureWarning)
#excel
df = pd.read_excel('FAQ.xlsx', index_col = None)
df = pd.read_excel('QAリスト.xlsx', index_col = None)
my_dic = pd.read_excel('FAQ.xlsx', index_col = 0).to_dict()
root = tk.Tk()
root.title('FAQチャット')
root.geometry('600x500')
notebook = ttk.Notebook(root)
tab_one = tk.Frame(notebook, bg='#202058')
tab_two = tk.Frame(notebook, bg='white')
tab_three = tk.Frame(notebook, bg='white')
notebook.add(tab_one, text='Q&A')
notebook.add(tab_two, text='回答集')
notebook.add(tab_three, text='未回答')
notebook.pack(expand=True, fill='both')
#tab_one
def talk(text):
que = str(text)
read_ex = df[df['質問'].str.contains(que)]
#質問がない時 if len(read_ex.index) == 0: mysay = 'you: ' + text print(mysay) label = tk.Label(ListBox1, text=mysay, bg='#202058', fg='white') label.pack(anchor='e', padx=3, pady=3) FAQ_chat= '質問がありません、質問を追加しますか?' Entry1.delete(0, tk.END) label = tk.Label(ListBox1, text=FAQ_chat, bg='white') label.pack(anchor='w', padx=3, pady=3) add_q() #該当した検索が1つの時 else: if len(read_ex.index) == 1: mysay = 'you: ' + text print(mysay) label = tk.Label(ListBox1, text=mysay, bg='#202058', fg='white') label.pack(anchor='e', padx=3, pady=3) try: FAQ_chat = str(read_ex['質問'].values[0]) FAQ_chat2 = str(read_ex['回答'].values[0]) FAQ_chat3 = str(read_ex['url'].values[0]) except ValueError: FAQ_chat = str(read_ex['質問'].values[0]) FAQ_chat2 = '回答がありません。' FAQ_chat3 = None index=read_ex.index.values[0] print(index) cur_num = df.loc[index]['検索数'] df.loc[[index], '検索数'] = cur_num+1 df.to_excel('QAリスト.xlsx', sheet_name='sheet1',index = False) Entry1.delete(0, tk.END) label = tk.Label(ListBox1, text='【質問】'+'\n'+FAQ_chat, bg='white') label.pack(anchor='w', padx=3, pady=3) label = tk.Label(ListBox1, text='【回答】'+'\n'+FAQ_chat2, bg='white') label.pack(anchor='w', padx=3, pady=3) label = tk.Label(ListBox1, text=FAQ_chat3, bg='white') label.pack(anchor='w', padx=3, pady=3) label.bind("<Button-1>", lambda e: callback("{[0]}".format(read_ex['url'].values))) #該当した検索が複数の時 else: que_ex = [] ans = [] for i in read_ex.index: que_ex.append(str(i)+' '+read_ex['質問'][i]) mysay = 'you: ' + text print(mysay) label = tk.Label(ListBox1, text=mysay, bg='#202058', fg='white') label.pack(anchor='e', padx=3, pady=3) Entry1.delete(0, tk.END) for i in range(len(que_ex)): label = tk.Label(ListBox1, text=que_ex[i], bg='white') label.pack(anchor='w', padx=3, pady=3) option(read_ex)
def option(read_ex):
input_num = simpledialog.askstring("Input Box", "番号を入力してください。")
if input_num in read_ex.index.values:
s=int(input_num)
print(type(s))
cur_num = df.loc[s]['検索数']
df.loc[[s], '検索数'] = cur_num+1
df.to_excel('QAリスト.xlsx', sheet_name='sheet1',index = False)
Entry1.delete(0, tk.END)
FAQ_chat = read_ex['質問'][s] FAQ_chat2 = read_ex['回答'][s] FAQ_chat3 = read_ex['url'][s] if FAQ_chat2 is np.nan: FAQ_chat2 = '回答がありません。' FAQ_chat3 = '' #質問 label = tk.Label(ListBox1, text='【質問】'+FAQ_chat, bg='white') label.pack(anchor='w', padx=3, pady=3) #回答 label = tk.Label(ListBox1, text='【回答】'+FAQ_chat2, bg='white') label.pack(anchor='w', padx=3, pady=3) #url label = tk.Label(ListBox1, text=FAQ_chat3, bg='white') label.pack(anchor='w', padx=3, pady=3) label.bind("<Button-1>", lambda e: callback("{[0]}".format(read_ex['url'][s]))) else: pass
def add_q():
mes = tkm.askyesno('確認', '質問がありません。質問を追加しますか?(y/n)')
if mes == True:
inputdata = simpledialog.askstring("Input Box", "追加する質問を入力してください。")
tmp = pd.Series( [df.index[-1]+2,None,str(inputdata),None,None,0], index=df.columns)
list_df = df.append(tmp,ignore_index=True)
list_df.to_excel('QAリスト.xlsx', sheet_name='sheet1',index = False)
FAQ_chat= str(inputdata)+' '+'質問が追加されました。'
Entry1.delete(0, tk.END)
label = tk.Label(ListBox1, text=FAQ_chat, bg='white') label.pack(anchor='w', padx=3, pady=3) else: FAQ_chat= '質問を追加しませんでした' Entry1.delete(0, tk.END) label = tk.Label(ListBox1, text=FAQ_chat, bg='white') label.pack(anchor='w', padx=3, pady=3)
#url
def callback(url):
webbrowser.open_new(url)
ListBox1 = tk.Listbox(tab_one, bg = 'gray82')
ListBox1.pack(expand=True, fill=tk.BOTH)
ListBox1.place()
xscroll = tk.Scrollbar(tab_one, orient=tk.HORIZONTAL, command=ListBox1.xview)
ListBox1["yscrollcommand"] = xscroll.set
ListBox1.grid(row=0, column=0)
xscroll.grid(row=1, column=0, sticky=(tk.N, tk.S))
xscroll.pack()
Entry1 = tk.Entry(tab_one, bg = 'white')
Entry1.insert(tk.END, '')
Entry1.pack(side = tk.LEFT, expand = True, fill = tk.BOTH, padx = 7, pady=3)
Entry1.place()
Button1 = tk.Button(tab_one, text = '送信', bg='RoyalBlue4', fg='white', command = lambda: talk(Entry1.get()))
Button1.pack(side = tk.RIGHT, padx = 4, pady=3)
Button1.place()
#tab_two
ListBox2 = tk.Listbox(tab_two, bg = 'white')
ListBox2.pack(expand=True, fill=tk.BOTH)
ListBox2.place()
Entry2 = tk.Entry(tab_two, bg = 'white')
Entry2.insert(tk.END, '')
Entry2.pack(side = tk.LEFT, expand = True, fill = tk.BOTH, padx = 5)
Entry2.place()
Button2 = tk.Button(tab_two, text = u'送信')
Button2.pack(side = tk.RIGHT)
Button2.place()
#tab_three
ListBox3 = tk.Listbox(tab_three, bg = 'white')
ListBox3.pack(expand=True, fill=tk.BOTH)
ListBox3.place()
Entry3 = tk.Entry(tab_three, bg = 'white')
Entry3.insert(tk.END, '')
Entry3.pack(side = tk.LEFT, expand = True, fill = tk.BOTH, padx = 5)
Entry3.place()
Button3 = tk.Button(tab_three, text = u'送信')
Button3.pack(side = tk.RIGHT)
Button3.place()
root.wm_attributes('-topmost', 1)
root.bind('<Escape>', lambda e: root.destroy())
root. mainloop()
あなたの回答
tips
プレビュー