我正试图使用组合框向用户询问一个稍后可以使用的参考点,并设法使组合框在测试脚本中正常工作,但当我将该脚本添加到代码的其余部分时,它停止了工作。
基本上:
- 我把一个excel文件读入了一个df
- 我通过获得";事件";柱
- 打开组合框并询问";事件";是";参考文献";,然后关上盒子
- 计划将使用";参考文献";指向对其他行进行计算
我目前正在使用在这里的另一篇文章中找到的部分代码,但我有一个问题。当我只使用代码的组合框部分时(将Events设置为['Monday','Tuesday','sThursday'…],它工作得很好。但是,当我将该组合框添加到代码的其余部分时,使用代码读取我的文件生成的事件列表,它就不再工作了(它似乎没有读取类部分?(。
请参阅下面的代码。
如果有你的帮助,我真的很高兴!
提前感谢!Orohena
#Import all library that will be needed
import os
import pandas as pd
from tkinter import *
from tkinter import filedialog
import tkinter as tk
from tkinter import ttk
#import matplotlib.pyplot as plt
#import numpy as np
#get result directory
def popupdir(msg):
# Set a font
large_font = ('Verdana', 20)
#popup window
popup = tk.Tk()
#title of the window
popup.wm_title('User action required')
#The window, plus the message, font to set the font of message, wrap text, justify to center.
label = ttk.Label(popup, text=msg, font=large_font, wraplength=500, justify='center')
#The text will be at the top of the window. padx, pady to make window bigger.
label.pack(side="top", padx=350, pady=100)
#Add a button to the popup window, the text of the button is okay. When click, popup window close.
Button1 = ttk.Button(popup, text="Okay", command = popup.destroy)
#This would be to modify aspect of button, keep initial settings here
Button1.pack()
#Keep window open until user do something.
popup.mainloop()
def ComboboxSelection():
class ComboboxSelectionWindow():
def __init__(self, master):
self.master = master
self.entry_contents = None
self.labelTop = tk.Label(master, text="Select one of the following")
self.labelTop.place(x=20, y=10, width=140, height=10)
self.comboBox_example = ttk.Combobox(master, values=events)
self.comboBox_example.current(0)
self.comboBox_example.place(x=20, y=30, width=140, height=25)
self.okButton = tk.Button(master, text='OK', command=self.callback)
self.okButton.place(x=20, y=60, width=140, height=25)
def callback(self):
self.comboBox_example_contents = self.comboBox_example.get()
self.master.destroy()
root = tk.Tk()
root.title('Ask a choice !')
root.geometry('400x400')
Selection = ComboboxSelectionWindow(root)
root.mainloop()
print("Selected interface: ", Selection.comboBox_example_contents)
return Selection.comboBox_example_contents
def folderfinder():
# Call popupdir function to warn users that they'll have to input something
popupdir('In the next window, you will need to choose the folder containing your results.')
# To ask the Tk empty box to immediately close
rootdir = Tk()
rootdir.withdraw()
# To ask the user for the directory
path = tk.filedialog.askdirectory()
# To close the directory window once the directory has been selected.
rootdir.update()
# Change directory to the one with the results
dir1 = os.chdir(path)
path1 = os.getcwd() + '/'
return(path1)
path1 = folderfinder()
#get the list of file in the result directory
file_list = [f for f in os.listdir(path1) if not f.startswith('.')]
dfeve = pd.read_csv(file_list[0], sep=',')
dftl = dfeve[['Events']]
#concatenate all file in one
for file in file_list:
nameframe = 'Frames' + file
nametimes = 'Times' + file
dffile = pd.read_csv(file, sep=',')
dffile.rename({'Frames_': nameframe}, axis=1, inplace=True)
dffile.rename({'Times_': nametimes}, axis=1, inplace=True)
dftl = dffile.merge(dftl, left_on='Events', right_on='Events')
events = dftl['Events']
events = events.tolist()
print(events)
print("combo")
ComboboxSelection()
print("comboprint")
print("Tkinter combobox text selected =", ComboboxSelection())
它工作良好,直到我点击";OK";在ComboboxSelection中,窗口关闭,代码仍在运行,但没有发生任何事情,它不打印任何内容。
我在ComboboxSelection中或外尝试了类,结果相同。
这是正在工作的代码,我点击";OK";,窗口关闭后,它打印出它应该打印的所有内容(从我的代码中;我再次尝试在ComboboxSelection中或之外使用该类,它在这两种情况下都有效(
import tkinter as tk
from tkinter import ttk
def ComboboxSelection():
class ComboboxSelectionWindow():
def __init__(self, master):
self.master = master
self.entry_contents = None
self.labelTop = tk.Label(master, text="Select one of the following")
self.labelTop.place(x=20, y=10, width=140, height=10)
self.comboBox_example = ttk.Combobox(master, values=options)
self.comboBox_example.current(0)
self.comboBox_example.place(x=20, y=30, width=140, height=25)
self.okButton = tk.Button(master, text='OK', command=self.callback)
self.okButton.place(x=20, y=60, width=140, height=25)
def callback(self):
""" get the contents of the Entry and exit"""
self.comboBox_example_contents = self.comboBox_example.get()
self.master.destroy()
root = tk.Tk()
root.title('Ask a choice !')
root.geometry('400x400')
Selection = ComboboxSelectionWindow(root)
root.mainloop()
print("Selected interface: ", Selection.comboBox_example_contents)
return Selection.comboBox_example_contents
options = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
print("Tkinter combobox text selected =", ComboboxSelection())
这个例子对你有帮助吗:
from tkinter import Tk, Button
from tkinter.ttk import Combobox
values = ['Option 1', 'Option 2', 'Option 3', 'Ōption 4']
def get_data():
print(cb.get())
root = Tk()
cb = Combobox(root, values=values)
cb.pack()
cb.set(values[0])
cb.bind('<<ComboboxSelected>>', lambda e: get_data())
root.mainloop()
在这种情况下,你甚至不需要一个按钮
你没有得到任何东西的原因可能是因为没有一个函数或类返回任何
你当然可以做的一件事是,在获得值后,在销毁窗口之前,尝试添加一个print(self.comboBox_example_contents)