Python Forum
[Tkinter] need help using a returned value to import module - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] need help using a returned value to import module (/thread-10648.html)

Pages: 1 2


RE: need help using a returned value to import module - Brave774 - May-30-2018

Yeah, pretty much, I’m still waiting for the pumps to arrive so when I know how long it takes to poor each I’ll adjust the timing


RE: need help using a returned value to import module - buran - May-31-2018

Did you make any progress?


RE: need help using a returned value to import module - Brave774 - May-31-2018

Some. I only had a couple hours last night and most of that was spent trying to get to grips with how the bit of code you gave me worked. I think I’ve got the jist of it I just need to get it to use the values from the radio buttons to produce the recipe instead of manually entering them


RE: need help using a returned value to import module - buran - May-31-2018

Here is something I did from your code. Of course you can go OOP and refactor it using classes. Also you may need to adjust the GPIO part (what you said will do anyway)


vending_gui.py

from tkinter import *
import vending

def make_label(title, row):
    lbl = Label(root,text=title, bg="black", fg="#FFEFDB",font="Times 32", width=14)
    lbl.grid(row=row, column=1)
    return lbl

def make_widget(title, items, row):
    lbl = make_label(title, row)
    buttons = []
    row += 1
    for col, drink in enumerate(items):
        radio_button = Radiobutton(root, selectcolor="grey", text=drink, variable=spiritvar, value=drink, 
                               bd=1, bg="#FFEFDB", relief="solid", font="Times 32", width=14)
        radio_button.config(indicatoron=0)
        column =  col%3
        radio_button.grid(row=row, column=column)
        buttons.append(radio_button)
        if column == 2:
            row += 1
    return (row, {'label':lbl, 'buttons':buttons})
    
def drink():
    vending.make_drink(spirit=spiritvar.get(), shots=shotsvar.get(), mixer=mixervar.get())

if __name__ == '__main__':
    root = Tk()
    root.geometry("1024x600+0+0")
    root.configure(bg = "black")
     
    spiritvar = StringVar()
    shotsvar = StringVar()
    mixervar = StringVar()
    widgets = []
    row = 0    
    for option, available_selections in vending.vending_options.items():
        row, widget = make_widget(title=option, items=available_selections, row=row)
        widgets.append(widget)
    b1 = Button(root, text="Make", bd=1, bg="white", relief="solid",
                font="Times 32", width=14, command=drink)
    b1.grid(row=row,column=1)
    root.mainloop()
vending.py
import RPi.GPIO as GPIO
import time
import sys
from collections import OrderedDict

shot_duration = 3
alcoholic_drinks = OrderedDict((('Vodka', 4), ('Gin',17), ('Whiskey',27)))
shots = OrderedDict((('Single', 1), ('Double',2), ('Triple',3)))
soft_drinks = OrderedDict((('Coke',22), ('Lemonade',18), ('Orange',23), ('Soda',24), ('Tonic',25), ('None', 0)))
vending_options = OrderedDict((('Spirits',alcoholic_drinks), ('Shots',shots), ('Mixers',soft_drinks)))

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)    

def make_drink(spirit, num_shots, mixer):
    for bevarage in [alchoholic_drinks, soft_drinks]:
        for pin in bevarage.values(): 
            GPIO.setup(pin, GPIO.OUT) 
            GPIO.output(pin, GPIO.HIGH)

    #prepare drink
    spirit = alcoholic_drinks[spirit]
    num_shots =  shots[num_shots]
    mixer = soft_drinks[mixer]
    GPIO.output(spirit, GPIO.LOW)
    if mixer:
        GPIO.output(mixer, GPIO.LOW)
    for i in range(4):
        time.sleep(shot_duration)
        if i == num_shots:
            GPIO.output(spirit, GPIO.HIGH)
    if mixer:
        GPIO.output(mixer, GPIO.HIGH)
    time.sleep(1)
    GPIO.cleanup()



RE: need help using a returned value to import module - Brave774 - May-31-2018

You absolute legend that will help me no end. I’ve got nothing planned for tonight except writing this code so I’ll let you know how I get on. Thanks


RE: need help using a returned value to import module - buran - May-31-2018

Note that vending.py is what you now have in 54 files. Also if want go fancy, you can (instead of hardcoding all options - alcoholic_drinks, shots, soft_drinks, vending_options, etc.) you can supply them as json file. In any case now it's easy to extend it as you like with more drinks, options, etc.


RE: need help using a returned value to import module - Brave774 - Jun-01-2018

Got it working last night. Just need to fix the timings for the drinks but seems to do what I need it to do. Thanks for all your help.