Python tkinerProgramm导入到新文件

2024-04-25 00:14:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我的tkinter gui的用户输出有问题。 首先,它可以工作,但是导入到一个新的py文件时有一个很大的问题。它抛出一个错误()。你知道吗

    Traceback (most recent call last):
  File "F:\Python_Projekt\Übung\Plus\test.py", line 4, in <module>
    tk = own.get_start()
  File "F:\Python_Projekt\Übung\Plus\plus_window_pack2.py", line 31, in get_start
    sel = ListBox1.curselection()
  File "C:\Users\nox\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2782, in curselection
    return self._getints(self.tk.call(self._w, 'curselection')) or ()
_tkinter.TclError: invalid command name ".!frame.!frame.!listbox"

我知道我的列表框有问题,但我不知道怎么处理。我需要我的新脚本使用的输出。所以我可以用它。你知道吗

from tkinter import*
import tkinter as tk

window = Tk()
rahmen1 = Frame(master = window) #, bg='black'
rahmen1.pack(side='top', padx=5, pady=5)
rahmen2 = Frame(master = rahmen1)  #, bg= 'yellow'
rahmen2.pack(side='left', padx=5, pady=5)


def get_start():
    selection=ListBox1.curselection()
    picked = selection[0]
    used = ListBox1.get(picked)              #-------user-input
    print(used)
    return used

####################----------------------------------------------------


List = ['Dax','Dow','EUR/USD', 'Gold', 'Silber','EUR/JPY','USD/JPY']

scrollbar1 = Scrollbar(rahmen2) # ,bg='green'
scrollbar1.pack(padx = 1,side = 'right',fill=Y)

ListBox1 = Listbox(rahmen2,exportselection = False)
ListBox1.config( yscrollcommand = scrollbar1.set, width = 40)
scrollbar1.config( command = ListBox1.yview) # ,bg='blue'
for i in List:
    ListBox1.insert(tk.END, str(i))
ListBox1.pack(padx = 1,)


###################------------------------------------------------

Button1 = Button(rahmen2,text='Get Data', font = 'bold')
Button1.config (width=40, height = 3, command = get_start)
Button1.pack( )


window.mainloop()

我修改了代码以得到重要的部分。为了更好地理解我的问题。 如果我想让用户输入,那就是我的错误。你知道吗


Tags: inpygettkinterwindowstartpacktk
1条回答
网友
1楼 · 发布于 2024-04-25 00:14:23

试试这个。我添加了一个简单的检查,以确保选择了某些内容(但我不确定问题顶部的错误是关于什么的):

def get_start():
    selection=ListBox1.curselection()
    if len (selection) != 0:
        picked = selection[0]
        used = ListBox1.get(picked)              #   -user-input
        print(used)
        return used

编辑: 这将每隔x秒(由UPDATE_TIME定义)检查复选框更新

UPDATE_TIME = 0.1


from tkinter import*
import tkinter as tk
import threading, time

window = Tk()
rahmen1 = Frame(master = window) #, bg='black'
rahmen1.pack(side='top', padx=5, pady=5)
rahmen2 = Frame(master = rahmen1)  #, bg= 'yellow'
rahmen2.pack(side='left', padx=5, pady=5)


def get_start():
    print (previous)
    return previous
def get_update ():
    global previous
    try:
        while True:
            selection=ListBox1.curselection()
            if len (selection) == 0: previous = None
            else:
                picked = selection[0]
                previous = ListBox1.get(picked)              #   -user-input
            time.sleep (UPDATE_TIME)
    except: pass

####################                          


List = ['Dax','Dow','EUR/USD', 'Gold', 'Silber','EUR/JPY','USD/JPY']

scrollbar1 = Scrollbar(rahmen2) # ,bg='green'
scrollbar1.pack(padx = 1,side = 'right',fill=Y)

ListBox1 = Listbox(rahmen2,exportselection = False)
ListBox1.config( yscrollcommand = scrollbar1.set, width = 40)
scrollbar1.config( command = ListBox1.yview) # ,bg='blue'
for i in List:
    ListBox1.insert(tk.END, str(i))
ListBox1.pack(padx = 1,)


###################                        

Button1 = Button(rahmen2,text='Get Data', font = 'bold')
Button1.config (width=40, height = 3, command = get_start)
Button1.pack ()

threading.Thread (target = get_update).start ()

window.mainloop()

相关问题 更多 >