2015-12-15 77 views
4

我真的需要一些代碼的幫助。我不指望你爲我寫這篇文章,因爲這是一個學校項目,但我真的很迷茫,需要幫助。Python Tkinter。如何通過標籤中的按鈕存儲條目

我寫的代碼是某種生產系統。
它並不需要真正能夠在任何地方發送任務,因爲這只是一個想象的場景。

代碼必須包含三個文件:data.py,model.py和gui.py.

  • 桂可以訪問其他兩個文件
  • 數據只能訪問模型
  • 型號不能訪問另外兩個任。

我的老師寫了一些我繼續寫的代碼。有些文字是丹麥文,但大多數評論都是英文的。代碼如下: 代碼如下。

data.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]) 

model.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

+0

'self.eq1'沒有定義。你有'self.q1_lbl'和'self.eq1_ent' – Zety

+1

當你調用'Entry.get()'它會得到那個確切時刻的內容。由於您在創建空條目後立即調用'.get()',它只會讓您獲得空字符串。 – Lafexlos

回答

2

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('text')

  • 檢查,如果你在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) 
    
+0

謝謝你的回答,其他人回答說,作爲評論,但刪除它,所以我糾正了這一點。我仍然不知道如何將標籤amount1,amount2以及所有q1-q3和a1-a3更改爲我在條目中編寫的內容。 –

+0

@Andreas Steiner我發現這個鏈接很有用http://effbot.org/tkinterbook – Zety