在python / tkinter

时间:2019-08-20 03:57:04

标签: python tkinter

我一直在尝试学习Python语言的乐趣,并决定最好通过一个项目来做。由于我在啤酒厂工作,因此我认为创建一个简单的GUI来显示工厂中的所有罐以及所盛放的每种啤酒及其当前状态会很酷。如果我双击一个战车,它将弹出一个新窗口,允许我编辑战车信息,但是我无法使子级的信息显示在父窗口中(更具体地说,我想更改父级子项上的组合框中列出列表的项)。这是一张基本显示我要完成的图像:

enter image description here

我认为这些值可能已正确传递到列表中,但没有更新为要显示的相应Widget。我尝试在函数上调用.update().after().mainloop(),但没有任何变化。
我尝试检查其他答案,但实际上我希望有一种无需求助于Classes即可解决此问题的方法,因为我还不太了解它们。这是我可以想到的最简单的代码最小版本:

import tkinter as tk  
from tkinter import ttk  
from PIL import Image, ImageTk  


def click_duplo_tfm(event):  # recognizes double-click on tanks and opens the specific window  
    if event.widget.extra == 'Widget TFM01':  
        abrir_janela('01', 1)  
    elif event.widget.extra == 'Widget TFM02':  
        abrir_janela('02', 2)  


def salvar_alteracoes():  
    print('THIS FUNCTION IS SUPPOSED TO PASS CHANGES TO PARENT WINDOW')  


def abrir_janela(num_tfm, ref):  # opens a new window to alter tank info  
    def manter_selecao_ceva(event):  
        global ceva_tqs  
        ceva_tqs[referencia] = opcao_ceva.get()  
        print(ceva_tqs[referencia])  

    def manter_selecao_status(event):  
        global status_tqs  
        status_tqs[referencia] = opcao_status.get()  
        print(status_tqs[referencia])  

    referencia = int(ref)  
    janela = tk.Toplevel(adega_grande)  
    janela.title('TFM-' + num_tfm)  
    janela.geometry('550x300')  
    tfm = Image.open(img_tqs[referencia])  
    tfm.thumbnail((300, 300), Image.ANTIALIAS)  
    photo_tfm = ImageTk.PhotoImage(tfm, master=janela)  
    label_tfm = tk.Label(image=photo_tfm, borderwidth=0, highlightthickness=0, master=janela)  
    label_tfm.image = photo_tfm  
    label_tfm.grid(column=1, rowspan=20, row=1)  
    qual_tfm = tk.Label(master=janela, text='TFM-' + num_tfm, font='Arial 24 bold')  
    qual_tfm.grid(column=2, row=1)  
    ceva = tk.Label(master=janela, text='Cerveja: ', font='Arial 20 bold')  
    ceva.grid(column=2, row=2, sticky='W')  
    ceva_selecionada = 'Ceva selecionada'  
    opcao_ceva = ttk.Combobox(master=janela, values=cervs_factory, state='readonly', textvariable=ceva_selecionada)  
    opcao_ceva.grid(column=3, row=2)  
    opcao_ceva.bind("<<ComboboxSelected>>", manter_selecao_ceva)  
    status = tk.Label(master=janela, text='Status: ', font='Arial 20 bold')  
    status.grid(column=2, row=3, sticky='W')  
    status_selecionado = 'Status selecionado'  
    opcao_status = ttk.Combobox(master=janela, values=('Fermentando', 'Maturando'), state='readonly', textvariable=status_selecionado)  
    opcao_status.grid(column=3, row=3)  
    opcao_status.bind("<<ComboboxSelected>>", manter_selecao_status)  
    botao_salvar_alteracoes = tk.Button(master=janela, text="Salvar alterações no TFM", command=salvar_alteracoes)  
    botao_salvar_alteracoes.grid(column=2, columnspan=2, row=4)  


# creates main window and tabs (there are 22 smaller tanks to be added later on second tab)  
gui = tk.Tk()  
gui.configure(background="white")  
gui.title("Acompanhamento de adegas")  
abas = tk.ttk.Notebook(gui)  
adega_grande = tk.Frame(abas)  
adega_pequena = tk.Frame(abas)  
abas.add(adega_grande, text='Adega Grande')  
abas.add(adega_pequena, text='Adega pequena')  
abas.grid()  

# list of all kinds of beer produced in the factory  
cervs_factory = ('VAZIO', 'Weiss', 'Vienna Lager')  

# list of images to show on the tanks  
img_tqs = ["",  
           "tq cheio Beer 1.png",    # TFM-01  
           "tq cheio Beer 2.png",       # TFM-02  
           ]              # TFM-12  

# list of which beer is currently on each tank  
ceva_tqs = ["",  
            cervs_factory[1],         # TFM-01  
            cervs_factory[2]]          # TFM-02  


# list of tank current status Fermenting/Maturing  
status_tqs = ["",  
              'Fermentando',    # TFM-01  
              'Maturando',      # TFM-02  
              ]              # TFM-12  


# ~~~~  TFM's Adega Grande  ~~~~ #  
font_titulo_tfm = 'Arial 14 bold'  # altera fonte e tamanho dos títulos dos TFM's  
font_cerv_e_status = 'Arial 12 bold'  # altera fonte e tamanho do tipo de cerveja e status dos TFM's  
tamanho_img_tqs = (250, 250)  # altera o tamanho das imagens dos TFM's, só considera o menor valor  

tfm01 = Image.open(img_tqs[1])  
tfm01.thumbnail(tamanho_img_tqs, Image.ANTIALIAS)  
photo_tfm01 = ImageTk.PhotoImage(tfm01)  
label_tfm01 = tk.Label(adega_grande, image=photo_tfm01, borderwidth=0, highlightthickness=0)  
label_tfm01.grid(column=1, row=0)  
legenda_tfm01 = tk.Label(adega_grande, text='TFM-01', font=font_titulo_tfm)  
legenda_tfm01.grid(column=1, row=1)  
ceva01 = tk.Label(adega_grande, text=ceva_tqs[1], font=font_cerv_e_status)  
ceva01.grid(column=1, row=2)  
status01 = tk.Label(adega_grande, text=status_tqs[1], font=font_cerv_e_status, fg='green')  
status01.grid(column=1, row=3)  
label_tfm01.bind('<Double-Button-1>', click_duplo_tfm)  
label_tfm01.extra = "Widget TFM01"  

tfm02 = Image.open(img_tqs[2])  
tfm02.thumbnail(tamanho_img_tqs, Image.ANTIALIAS)  
photo_tfm02 = ImageTk.PhotoImage(tfm02)  
label_tfm02 = tk.Label(adega_grande, image=photo_tfm02, borderwidth=0, highlightthickness=0)  
label_tfm02.grid(column=2, row=0)  
legenda_tfm02 = tk.Label(adega_grande, text='TFM-02', font=font_titulo_tfm)  
legenda_tfm02.grid(column=2, row=1)  
ceva02 = tk.Label(adega_grande, text=ceva_tqs[2], font=font_cerv_e_status)  
ceva02.grid(column=2, row=2)  
status02 = tk.Label(adega_grande, text=status_tqs[2], font=font_cerv_e_status, fg='blue')  
status02.grid(column=2, row=3)  
label_tfm02.bind('<Double-Button-1>', click_duplo_tfm)  
label_tfm02.extra = "Widget TFM02"  

abas.mainloop()    

如果我的问题/代码过长,我希望能被原谅,我对编码非常陌生,这是我第一次寻求帮助。英文也不太流利,但是我想你们在这里明白了我的问题。预先感谢您的任何见解。

1 个答案:

答案 0 :(得分:1)

如果使用类,确实确实容易得多,但是使用当前方法仍然可以实现。

基本上,您首先需要将品牌/状态标签存储到容器中(例如list),然后在生成Toplevel小部件时传递索引,以便您知道需要哪些标签修改。下面是一个最小样本:

import tkinter as tk
from tkinter import ttk
from itertools import cycle

root = tk.Tk()
brands = ('VAZIO', 'Weiss', 'Vienna Lager')
status = ("Fermenting","Maturing")
beer_factory = cycle(brands)
beer_status = cycle(status)
beer = []

for i in range(6):
    a = tk.Label(root,text=f"TFM-0{i+1}",width=20)
    a.grid(row=0,column=i)
    b = tk.Label(root, text=next(beer_factory))
    b.grid(row=1, column=i)
    c = tk.Label(root, text=next(beer_status))
    c.grid(row=2, column=i)
    a.bind('<Double-Button-1>', lambda e, i=i: toplevel(e,i))
    beer.append([a,b,c])

def toplevel(event, i):
    top = tk.Toplevel()
    tk.Label(top,text="Beer factory").grid(row=0,column=0)
    tk.Label(top, text="Status").grid(row=1, column=0)
    com1 = ttk.Combobox(top,values=brands,state="readonly")
    com1.set(beer[i][1]["text"])
    com1.grid(row=0,column=1)
    com2 = ttk.Combobox(top,values=status,state="readonly")
    com2.set(beer[i][2]["text"])
    com2.grid(row=1,column=1)
    def finish():
        beer[i][1].config(text=com1.get())
        beer[i][2].config(text=com2.get())
        top.destroy()
    tk.Button(top,text="Finish",command=finish).grid(row=3,column=0,columnspan=2)

root.mainloop()

<Double-Button-1>事件绑定到标签TFM-0X。