简体   繁体   English

我无法在我的 Python 项目中使用多线程

[英]I am having trouble getting multithreading working in my Python project

I am having trouble getting the threading to work in my code.我无法让线程在我的代码中工作。 If I take the threading out the for loop appends the menu.txt to menu = [].如果我取出线程,for循环会将menu.txt附加到menu = []。 For my homework I need to change this so that it is working with a thread.对于我的家庭作业,我需要更改它以使其与线程一起使用。 This is what I have so far but I keep coming up with an exception 'module' object is not callable.这是我到目前为止所拥有的,但我一直想出一个异常“模块”object 不可调用。 I feel like I am close but I am missing something.我觉得我很接近,但我错过了一些东西。

This is itemClass.py -这是 itemClass.py -

class item(Thread): #class for the menu items
    def __init__(self, name, wholeSale, retail, orderCount):
        Thread.__init__(self)
        self.name = name
        self.wholeSale = wholeSale
        self.retail = retail
        self.orderCount = orderCount

This is guiMain.py -这是 guiMain.py -

from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from PIL import ImageTk,Image
import manager
import restaurant
import itemClass
import threading
from threading import *

if __name__ == "__main__":

    menu = []

    t1 = itemClass.item()
    t1.start()

    with open("menu.txt") as f: #reads menu.txt to store in menu variable
        for line in f.readlines():
            arr = line.split(",")
            menu.append(t1(arr[0],float(arr[1]),float(arr[2]),int(arr[3])))

mainGUI(menu) mainGUI(菜单)

This is menu.txt -这是 menu.txt -

Chicken Sandwich,3.5,4.99,16
Spicy Chicken Sandwich,3.75,5.49,21
Chicken Tender Box,4.17,5.99,22
Spicy Tender Box,4.38,6.49,10
Fries,1.15,2.99,22

The Exception Being Thrown -抛出的异常 -

Message=item.__init__() missing 4 required positional arguments: 'name', 
'wholeSale', 'retail', and 'orderCount'

Source=C:\Users\neodr\Desktop\PythonClass\Lesson10_2051495\Lesson10Project2_2051495\guiMain.py StackTrace: File "C:\Users\neodr\Desktop\PythonClass\Lesson10_2051495\Lesson10Project2_2051495\guiMain.py", line 46, in (Current frame) t1 = itemClass.item() Source=C:\Users\neodr\Desktop\PythonClass\Lesson10_2051495\Lesson10Project2_2051495\guiMain.py StackTrace:文件“C:\Users\neodr\Desktop\PythonClass\Lesson10_2051495\Lesson10Project2_2051495\guiMain.py”,第 46 行,在(当前框架) t1 = itemClass.item()

The error you describe is a basic one, and doesn't actually have anything to do with threading per se.您描述的错误是一个基本错误,实际上与线程本身没有任何关系。 The itemClass.item class you've defined expects to be given four arguments.您定义的itemClass.item class 期望得到四个 arguments。 You're not doing that when you try to create an instance of the class in your GUI code.当您尝试在 GUI 代码中创建 class 的实例时,您并没有这样做。 When you do t1 = itemClass.item() , you're not passing any arguments.当您执行t1 = itemClass.item()时,您不会传递任何 arguments。 Later in your code you try to call t1 with arguments, but that's not supported either (an instance of your class is not callable).稍后在您的代码中,您尝试使用 arguments 调用t1 ,但这也不支持(您的 class 的实例不可调用)。

None of this makes much sense.这些都没有多大意义。 I suspect the underlying problem is that you're trying to insert threading into a place it doesn't make any sense.我怀疑潜在的问题是你试图将线程插入一个没有任何意义的地方。 You should want a thread to be running where you have ongoing stuff happening and you want it to continue to happen while you're doing something else in the main thread.您应该希望一个线程在您正在进行的事情发生的地方运行,并且您希望它在您在主线程中执行其他操作时继续发生。 The part of your code that you've shown doesn't seem like it matches up with that.您显示的代码部分似乎与它不匹配。 You're just defining menu items here.您只是在这里定义菜单项。 There's nothing ongoing that needs a thread.没有任何正在进行的事情需要线程。

It is likely that you need to rethink the structure of this code and put the threading somewhere else.您可能需要重新考虑此代码的结构并将线程放在其他地方。 For instance, it might make sense for a menu item, once selected, to start up a thread that keeps running in the background.例如,菜单项一旦被选中,就可以启动一个在后台持续运行的线程。 But the thread creation would then be triggered by GUI stuff (selecting from the menu), not created and started up front when the menu items are being defined.但是线程创建将由 GUI 内容(从菜单中选择)触发,而不是在定义菜单项时预先创建和启动。

1.you havent defined itemClass anywhere so i assume you ment item so try: from itemClass import item 1.你还没有在任何地方定义 itemClass 所以我假设你 ment item 所以尝试: from itemClass import item

 t1 = item() 

2.you havent passed any arguments to init method of item class 2.you have not pass any arguments to init item of class

3.it is not clear what are you trying to achive with the thread 3.不清楚你想用线程实现什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM