首页 > 解决方案 > Python 3.8.2:filedialog.askopenfilename 和 ttk.Entry 冲突

问题描述

我正在尝试从excel文件中读取列表并将其放入组合框中,但是当我执行下面的代码时,无法用键盘填充条目,我应该先使用组合框或更改窗口并切换回激活条目并填写它。

代码和excel文件在下面的链接中。

import tkinter as tk 
from tkinter import ttk
from tkinter import filedialog
import pandas

win = tk.Tk()
win.title('Label Frame ')
screen_width = int(win.winfo_screenwidth()/2)
screen_height = int(win.winfo_screenheight()/2)
win.geometry(f"{screen_width}x{screen_height}+0+0")

File_opned = filedialog.askopenfilename(title='Please select excel file.',
                                    filetypes=[('excel File',['.xls', '.xlsx', '.xlsm', '.xltx','.xltm', '.xlam'])])
excel_data_df = pandas.read_excel(File_opned, sheet_name='Scenes')
file_Excel_list = excel_data_df['Scenes'].tolist()

label_frame = tk.LabelFrame(win, text='Test Infos ', bd=3, padx=10, pady=10)
label_frame.grid(row=0, column=0, padx=40)


cur_label1 = ttk.Label(label_frame, text = 'What is your name : ')
cur_label1.grid(row=0, column=0)

cur_label2 = ttk.Label(label_frame, text = 'Scenes : ')
cur_label2.grid(row=1, column=0)


cur_entrybox = ttk.Entry(label_frame, width=19, textvariable=tk.StringVar())
cur_entrybox.grid(row=0, column=1)

cur_combobox = ttk.Combobox(label_frame, width=16, values=file_Excel_list)
cur_combobox.grid(row=1, column=1)

def submit():
    print(cur_entrybox.get())
    print(cur_combobox.get())

submit_btn = ttk.Button(win, text='Submit', command=submit)
submit_btn.grid(row=1, columnspan=2)

for child in label_frame.winfo_children():
    child.grid_configure(padx=4, pady=4)

win.mainloop()

excel文件和源代码

标签: python-3.xtkinter

解决方案


推荐阅读