python tkinter combobox/button

Question:

I just started to use python and tkinter, I have created a gui with two combobox and ‘run’ buttons which contain two different options ‘SPMI’ and ‘RFFE’. What I want my script to do is, when I select different options and click ‘run’ it should run the SPMI.py or RFFE.py file.

Help please, Thanks

My code:

import sys
from Tkinter import *



def callback1():
    os.system('SPMI.py')

def callback2():   
    os.system('RFFE.py')

class MyOptionMenu(OptionMenu):
     def __init__(self, master, status, *options):
          self.var = StringVar(master)
          self.var.set(status)
          OptionMenu.__init__(self, master, self.var, *options)
          self.config(font=('calibri',(10)),bg='white',width=12,fg='dark red')
          self['menu'].config(font=('calibri',(10)),bg='white',fg='dark blue')

          b1_1 = Button(Dragonfly, text = "Run", fg='blue',command=callback1)
          b1_1.place(x=85,y=150)


         b2_2= Button(Dragonfly, text = "Run", fg='blue',command=callback2)
         b2_2.place(x=275,y=150)


Dragonfly = Tk()
Dragonfly.geometry('400x400+400+300')
Dragonfly.title('Dragonfly')
mainlabel = Label(text='Dragonfly Trigger Test',font=('calibri',(14)),fg='dark blue').pack()


mymenu1 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI','RFFE')
mymenu2 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI','RFFE')
mymenu1.place(x=40,y=100)
mymenu2.place(x=230,y=100)
m1label = Label(text='Frame Trigger',font=('calibri',(12)),fg='dark green').place(x=57,y=60)
m1labe2 = Label(text='External Trigger',font=('calibri',(12)),fg='dark green').place(x=240,y=60)

Dragonfly.mainloop()
Asked By: user2871897

||

Answers:

Use self.var.get() to get the value of the combobox.
You can then use that value inside the callback to make the callback’s behavior depend on the combobox’s setting.


import sys
from Tkinter import *
import subprocess

class MyOptionMenu(OptionMenu):
    def __init__(self, master, status, *options):
        self.var = StringVar(master)
        self.var.set(status)
        OptionMenu.__init__(self, master, self.var, *options)
        self.config(
            font=('calibri', (10)), bg='white', width=12, fg='dark red')
        self['menu'].config(font=('calibri', (10)), bg='white', fg='dark blue')

    def callback(self):
        val = '{}.py'.format(self.var.get())
        print(val)
        # subprocess.call([val])

Dragonfly = Tk()
Dragonfly.geometry('400x400+400+300')
Dragonfly.title('Dragonfly')
mainlabel = Label(text='Dragonfly Trigger Test', font=('calibri', (14)),
                  fg='dark blue').pack()

mymenu1 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI', 'RFFE')
b1_1 = Button(Dragonfly, text="Run", fg='blue', command=mymenu1.callback)
b1_1.place(x=85, y=150)

mymenu2 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI', 'RFFE')
b2_2 = Button(Dragonfly, text="Run", fg='blue', command=mymenu2.callback)
b2_2.place(x=275, y=150)

mymenu1.place(x=40, y=100)
mymenu2.place(x=230, y=100)
m1label = Label(text='Frame Trigger', font=('calibri', (12)),
                fg='dark green').place(x=57, y=60)
m1labe2 = Label(text='External Trigger', font=('calibri', (12)),
                fg='dark green').place(x=240, y=60)

Dragonfly.mainloop()

By the way, building a GUI with place is easy at first, but cumbersome in the end. For one thing, as it grows it become harder and harder to change the layout since inserting a new widget will tend to require fiddling with hard-coded coordinates all over the place. Another problem is that the widget layout fails to adjust to changes in window size.

For those reasons, people tend to use pack or grid to layout widgets:

import sys
import Tkinter as tk
import subprocess

class MyOptionMenu(tk.OptionMenu):
    def __init__(self, master, status, *options):
        self.var = tk.StringVar(master)
        self.var.set(status)
        tk.OptionMenu.__init__(self, master, self.var, *options)
        self.config(
            font=('calibri', (10)), bg='white', width=12, fg='dark red')
        self['menu'].config(font=('calibri', (10)), bg='white', fg='dark blue')

    def callback(self):
        val = '{}.py'.format(self.var.get())
        print(val)
        # subprocess.call([val])

Dragonfly = tk.Tk()
Dragonfly.geometry('400x400+400+300')
Dragonfly.title('Dragonfly')
Dragonfly.columnconfigure(0, weight=1)
Dragonfly.columnconfigure(1, weight=1)  
mainlabel = tk.Label(text='Dragonfly Trigger Test', font=('calibri', (14)),
                  fg='dark blue').grid(row=0, column=0, columnspan=2, pady=20)

mymenu1 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI', 'RFFE')
mymenu1.grid(row=2, column=0, pady=10, padx=10,)
b1_1 = tk.Button(Dragonfly, text="Run", fg='blue', command=mymenu1.callback)
b1_1.grid(row=3, column=0, pady=10, padx=10,)

mymenu2 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI', 'RFFE')
mymenu2.grid(row=2, column=1, pady=10, padx=10,)
b2_2 = tk.Button(Dragonfly, text="Run", fg='blue', command=mymenu2.callback)
b2_2.grid(row=3, column=1, pady=10, padx=10,)

m1label = tk.Label(text='Frame Trigger', font=('calibri', (12)),
                fg='dark green').grid(row=1, column=0, pady=10, padx=10,)
m1labe2 = tk.Label(text='External Trigger', font=('calibri', (12)),
                fg='dark green').grid(row=1, column=1, pady=10, padx=10,)

Dragonfly.mainloop()
Answered By: unutbu

Que– if I want to open further file from that select protocol box for that if I apply if else condition, how do we apply pls reply.

Answered By: tushar nampalliwar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.