Python;无法调用列表框

标签 python tkinter invoke raspberry-pi

我是个十足的新手。上周我开始使用 python,尝试创建一个功能性 GUI。该 GUI 将使用 ttk 笔记本使用多个选项卡。但是,我在程序中添加多个列表框时遇到了问题。我确实有一个工作列表框,位于“tab11”中。但是,每当我尝试添加一个简单的列表框时,它在运行程序时都不会呈现。然而 shell 给我提供了一个错误,我不知道问题是什么。

我得到的错误是:

Traceback (most recent call last):
  File "/Users/nielsschreuders/Documents/01_TUe_B32_Feb13_Jun13/Project/Concept/prototype/python/RaspberryBackup/B32Project/PythonExperiments/multipleListboxTest.py", line 133, in <module>
    listbox2 = tk.Listbox(tab12, width=50, height=-1)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 2608, in __init__
    Widget.__init__(self, master, 'listbox', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 2075, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: can't invoke "listbox" command:  application has been destroyed

这是代码的简化版本:

import tkinter as tk
#import RPi.GPIO as gpio
import sys
import os

from tkinter import ttk


mGui = tk.Tk()
mGui.geometry('800x480')
mGui.title("Apps on Wheels")

nb = ttk.Notebook(mGui, name="notebook")
nb.pack(fill=tk.BOTH, expand=tk.Y, padx=2, pady=3)

dispColor="Yellow"

#create tabs
tab1 = ttk.Frame(nb, name="audioTab")


#assign tabs to notebook children
nb.add(tab1, text="Audio")



nbAS = ttk.Notebook(tab1, name="audioSource")
nbAS.pack(pady=100)
#nbAS.pack(fill=tk.BOTH, expand=tk.Y, padx=2, pady=3)

tab11 = ttk.Frame(nbAS, name="radioTab")
tab12 = ttk.Frame(nbAS, name="mp3Tab")

nbAS.add(tab11, text="Radio")
nbAS.add(tab12, text="MP3")

##################Radio Controller###################



def get_list(event):
    #get selected line index
    index = listbox1.curselection()[0]

    #get the line's text
    seltext=listbox1.get(index)

    #delete previous text in enter1
    enter1.delete(0, 50)

    #display selected text
    enter1.insert(0, "You are listening to: " +seltext)

    intIndex = int(index)
    intRadioFreq = intIndex +1
    radioFreq = str(intRadioFreq)
    os.system("mpc play " + radioFreq)

def set_list(event):

    try:
        index = listbox1.curselection()[0]
        #delete old listbox line
        listbox1.delete(index)

    except IndexError:
        index = tk.END
    #insert edited item back into listbox1 at index
    listbox1.insert(index, enter1.get())
def sort_list():
    temp_list = list(listbox1.get(0, tk.END))
    temp_list.sort(key=str.lower)
    #delete contents of present listbox
    listbox1.delete(0, tk.END)
    for item in temp_list:
        listbox1.insert(tk.END, item)


def save_list():

    #get a list of listbox lines
    temp_list = list(listbox1.get(0, tk.END))
    #add a trailing newline char to each line
    temp_list=[radio + '\n' for radio in temp_list]
    #give the file new name
    fout = open("radio_data2.txt", "w")
    fout.writelines(temp_list)
    fout.close()


#create the data file
str1 = """Radio 1
Radio 2
3FM Serious Radio
538 Radio
Radio 6 Soul & Jazz
"""

fout = open("radio_data.txt", "w")
fout.write(str1)
fout.close()

fin = open("radio_data.txt", "r")
radio_list= fin.readlines()
fin.close()
radio_list= [radio.rstrip() for radio in radio_list]

RadioFrequencies = tab11

#creat listbox
listbox1 = tk.Listbox(RadioFrequencies , width=50, height=-1)
listbox1.grid(row=0, column=0)


#use entry widget to display/edit selection

enter1 = tk.Entry(RadioFrequencies , width=44, bg=dispColor, fg="black", justify=tk.CENTER, font=("Arial" ,16, "bold"))
#enter1.insert(0, "Choose your radio station")

enter1.grid(row=1, column=0)


for item in radio_list:
    listbox1.insert(tk.END, item)

listbox1.bind("<ButtonRelease-1>", get_list)


RadioFrequencies.mainloop()



listbox2 = tk.Listbox(tab12, width=50, height=-1)
listbox2.place(x=100, y=150)


listbox2.insert(tk.END, "entry here")
for item2 in ["one", "two", "three", "four"]:
    listbox2.insert(tk.END, item2)

mGui.mainloop()

最佳答案

您的程序在两个地方调用 mainloop():

RadioFrequencies.mainloop()
...
mGui.mainloop()

一般来说,Tkinter 应用程序应该只调用 mainloop 一次,并且通常是 tk.Tk() 实例进行调用。一旦进行调用,程序就会将控制流移交给管理所有 GUI 事件的 Tkinter 事件循环。

通常,对 mainloop() 的调用将是脚本中的最后一个调用。当 GUI 退出时,脚本也会退出。

所以删除该行

RadioFrequencies.mainloop()
<小时/>

您收到错误的原因

_tkinter.TclError: can't invoke "listbox" command:  application has been destroyed

是因为关闭GUI后,Python会从

返回
RadioFrequencies.mainloop()

然后尝试调用

listbox2 = tk.Listbox(tab12, width=50, height=-1)

但此时申请已经结束。

关于Python;无法调用列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16752707/

相关文章:

python - 尝试使用 stdin : io. UnsupportedOperation 时在 Python 中出现错误:fileno

python - max() 的不同面孔

python - 解码 os.urandom() 字节对象

python - Python 中 'wb' 文件模式下的 FileNotFoundError?

python - 自动启动时 Tkinter GUI 无法正常运行

python - 如何在 tkinter.Text 小部件中显示行号?

python - 属性错误两类

c# - 如何捕获使用 MethodInfo.Invoke 调用的方法中抛出的异常?

c# - 从类中调用方法

c# - 如何调用带有空参数的委托(delegate)?