gpt4 book ai didi

Python Tkinter。如何通过标签中的按钮存储条目

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:47 26 4
gpt4 key购买 nike

我真的需要一些代码方面的帮助。我不希望你为我写它,因为这是一个学校项目,但我真的迷路了,需要帮助。

我正在编写的代码是某种生产系统。
它不需要真正能够将任务发送到任何地方,因为这只是一个想象的场景。

代码必须包含三个文件:data.py、model.py 和gui.py。

  • Gui 可以访问另外两个文件
  • 数据只能访问模型
  • 模型无法访问其他两个中的任何一个。

我的老师写了一些我继续写的代码。一些文本是丹麦语,但大多数评论是英文的。代码如下。

数据.py

from model import *

class Data(object):

def __init__(self):
self.units = []
self.finished_tasks = []

def __str__(self):
result = "These tasks have been finished: "
for i in self.finished_tasks:
result += str(i)
return result

def task_done(self, unit):
done_task = unit.task_done()
if done_task != None:
#TODO: add to list of finished tasks
pass

def add_task(self, name, amount, unit):
s = Springroll_task(name, amount)
unit.add_to_queue(s)

def read_from_database(self):#doesn't actually read from db..
self.units.append(Production_unit("maskine1"))
self.units.append(Production_unit("maskine2"))

self.add_task("Miniruller", 100, self.units[0])
self.add_task("Maxiruller", 200, self.units[0])
self.add_task("HowIRoll", 3000, self.units[0])
self.add_task("RulleMarie", 40, self.units[1])
self.add_task("Rullesten", 500, self.units[1])
self.add_task("Toiletpapirsruller", 6000, self.units[1])

模型.py

    class Springroll_task(object):

def __init__(self, name, amount):
self.name = name
self.amount = amount

def __str__(self):
return self.name + " " + str(self.amount)


class Production_unit(object):

def __init__(self, amount={}, name={},):
#name of the production unit
self.name = name
self.amount = amount
#the current task
self.current_task = None
#the tasks in the queue
self.springroll_queue = []
#the size of the queue
self.queue_size = 0

def __str__(self):
#TODO
return self.name + " " + str(self.amount)

def add_to_queue(self, task={}):
if self.current_task == None:
self.current_task = task
else:
self.springroll_queue.append(task)
self.queue_size += 1
#remember to update queue_size
pass

def task_done(self):
#TODO: remember the old current task.
#Set the current task to be the first in the queue (and remove from queue)
# - if there is a task in the queue.
#return the old current task.
#remember to update queue_size
self.queue_size -= 1
pass

gui.py

from tkinter import *
from model import *
from data import Data

class Application(Frame):
def __init__(self, master, unit):
self.mod = Production_unit()
super(Application, self).__init__(master)
self.grid()
self.unit = unit
self.create_widgets()



def create_widgets(self):
self.unit_name_lbl = Label(self, text = self.unit.name)
self.unit_name_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W)

self.cur_prod_lbl = Label(self, text = "produktion nu: ")
self.cur_prod_lbl.grid(row = 1, column = 0, columnspan = 2, sticky = W)

self.prod_lbl = Label(self, text = "produkt")
self.prod_lbl.grid(row = 2, column = 0, sticky = W)

self.amount_lbl = Label(self, text = "antal")
self.amount_lbl.grid(row = 2, column = 1, sticky = W)
#Label for production now
self.amount1_lbl = Label(self, text = " ", bg ="red")
self.amount1_lbl.grid(row = 3, column = 0, sticky = W)

self.amount2_lbl = Label(self, text = " ", bg ="red")
self.amount2_lbl.grid(row = 3, column = 1, sticky = W)
#Button for task finished
self.finished_but = Button(self, text = "Opgave afsluttet", bg ="pink", command=self.mod.task_done)
self.finished_but.grid(row = 3, column = 2, sticky = W)

#Label for queue
self.queue_lbl = Label(self, text = "Kø")
self.queue_lbl.grid(row = 4, column = 0, sticky = W)
#Label for production queue
for i in range(0,3):
self.name_lbl =Label(self, text = self.mod.springroll_queue, bg="red", width= 6)
self.name_lbl.grid(row = 5+i, sticky = W)

for j in range(0,3):
self.qt_lbl =Label(self, text = self.mod.springroll_queue, bg="red", width= 4)
self.qt_lbl.grid(row = 5+j, column = 1)

self.new_lbl = Label(self, text = "Ny")
self.new_lbl.grid(row = 10, column = 0, sticky = W)
#Entry for entries
self.eq1_ent = Entry(self, text = "", width=6)
self.entry_name = self.eq1_ent.get()
self.eq1_ent.grid(row = 11, sticky = W)

self.ea1_ent = Entry(self, text = "", width=4)
self.ea1_ent.grid(row = 11, column = 1, sticky = W)
#Button for add to queue
self.add_but = Button(self, text = "Tilføj til kø", bg ="pink", command=self.mod.add_to_queue(self.ea1_ent.get()))
self.add_but.grid(row = 11, column = 2, sticky = W)





def done(self):
d.task_done(self.unit)
self.redraw()

def add(self):
n = "Nyt navn" #read from gui
a = "Nyt antal" #read from gui
d.add_task(n, a, unit)
self.redraw()


def redraw(self):
#TODO
pass

# main
root = Tk()
root.title("Daloon")
root.geometry("300x300")

d = Data()

d.read_from_database()

p = d.units[0]

app = Application(root, p)

root.mainloop()

所以现在看起来是这样的:

我需要做的是在底部的两个条目小部件中获取输入并将它们放入上面的 4 个标签小部件之一中,从顶部开始然后在队列中,这应该发生在我按下按钮 add_but,它目前似乎已经消失了。
之后,当按下“Opgave afsluttet”按钮时,我需要将任务存储在数据文件中。

我真的希望有人能帮助我!

我根据一些建议对其进行了编辑,我认为现在正在调用正确的 self.eq1_ent.get() 。我不再收到任何错误,现在我真的不知道如何让它做我想做的事。编辑 2:我正在慢慢得到一些东西,所以我对 model.py 和 gui.py 进行了更改......现在看起来像这样: image

最佳答案

self.eq1 未定义。你有 self.q1_lblself.eq1_ent
要访问标签,请使用 self.q1_lbl

为了能够为您的标签设置文本,请按以下方式创建它们:

self.var = StringVar()
self.unit_name_lbl = Label(self, textvariable=self.var)

例如,从 redraw() 中,您可以将 'text' 设置为 self.unit_name_lbl,如下所示:self.var .set('文本').

  • 检查您是否在 d.add_task(n, a, unit)

    中错过了 self
  • 当您执行 command=mod.add_to_queue(self.ea1_ent.get()) 时,如果您需要,将直接调用 mod.add_to_queue 函数要在用户按下按钮时将参数传递给此函数,您可以使用 lambda:

    command=lambda: mod.add_to_queue(self.ea1_ent.get)

关于Python Tkinter。如何通过标签中的按钮存储条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34290149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com