0

I decided to try my hand at Python and Tkinter as a first project. I am having issues with getting my variables to change when I click the relevant buttons. I have searched for 3 days so far and haven't found exactly what I need in order to execute my 'def Next' part of my program. Sorry my code is a mess. Any tips and shortcuts for coding this are appreciated. The end result of the code should execute for example iwconfig wlan1 txpower 20.

#!/usr/bin/python
# First Python & Tkinter project
# K. Silva

# Setup interface buttons and functions
global interface
interface = "wlan0"
global cardpower
cardpower = "0"

import os
# Clear screen
os.system("clear")
# Set geo for wireless card power
os.system("sudo iw reg set BO")
# List interfaces
os.system("iwconfig")

# Begin GUI coding

from Tkinter import *

master = Tk()
master.title('Wireless Warrior')

def Wla0():
  interface = "wlan0"
  print interface

def Wla1():
  interface = "wlan1"
  print interface

def Wla2():
  interface = "wlan2"
  print interface

def Wla3():
  interface = "wlan3"
  print interface

def Wla4():
  interface = "wlan4"
  print interface

def Wla5():
  interface = "wlan5"
  print interface

def Wla6():
  interface = "wlan6"
  print interface

w = Label(master, text="Wireless Warrior", fg="blue", font=("Helvetica", 16))
w.grid(row=0, column=0, columnspan=3)

Wl0 = Button(master, text="Wlan0", width=4, command=Wla0)
Wl0.grid(row=1,column=0)

Wl1 = Button(master, text="Wlan1", width=4, command=Wla1)
Wl1.grid(row=2,column=0)

Wl2 = Button(master, text="Wlan2", width=4, command=Wla2)
Wl2.grid(row=3,column=0)

Wl3 = Button(master, text="Wlan3", width=4, command=Wla3)
Wl3.grid(row=4,column=0)

Wl4 = Button(master, text="wlan4", width=4, command=Wla4)
Wl4.grid(row=5,column=0)

Wl5 = Button(master, text="wlan5", width=4, command=Wla5)
Wl5.grid(row=6,column=0)

Wl6 = Button(master, text="wlan6", width=4, command=Wla6)
Wl6.grid(row=7,column=0)

# Setup power buttons and functions



def Half():
  cardpower = "20"
  print cardpower

def One():
  cardpower = "30"
  print cardpower

def OneFive():
  cardpower = "31"
  print cardpower

def Two():
  cardpower = "33"
  print cardpower

def Three():
  cardpower = "34"
  print cardpower

def Five():
  cardpower = "36"
  print cardpower

def FiveEight():
  cardpower = "37"
  print cardpower

half = Button(master, text="500mW", width=4, command=Half)
half.grid(row=1,column=1)

one = Button(master, text="1W", width=4, command=One)
one.grid(row=2,column=1)

onefive = Button(master, text="1.5W", width=4, command=OneFive)
onefive.grid(row=3,column=1)

two = Button(master, text="2W", width=4, command=Two)
two.grid(row=4,column=1)

three = Button(master, text="3W", width=4, command=Three)
three.grid(row=5,column=1)

five = Button(master, text="5W", width=4, command=Five)
five.grid(row=6,column=1)

fiveeight = Button(master, text="5.8W", width=4, command=FiveEight)
fiveeight.grid(row=7,column=1)

def Next():
  print "iwconfig " + interface + " txpower " + cardpower 
  #os.system("sudo iwconfig " + interface + " txpower " + (cardpower))

next = Button(master, text="Next",width=4, command=Next)
next.grid(row=7,column=3)


mainloop()
  • I'm not really sure what the problem is... When you click the Next button it executes the `Next` function like I expect you want. – korylprince Aug 27 '13 at 02:08
  • Also, you might want to look at http://stackoverflow.com/questions/6920302/passing-argument-in-python-tkinter-button-command . You can cut down on the amount of functions you're using by passing an argument to the command. – korylprince Aug 27 '13 at 02:17
  • Thanks for all of the tips. I have been studying the example code. I am now learning python from the ground up. The last time I programmed was on a commodore 64 in the 80's with Basic, hehe. I started on python and tkinter less than a week ago. I will get my programming chops down and continue to visit the site for more tips. – user2719869 Aug 28 '13 at 21:15

1 Answers1

0

Variables within functions are local to the function so the new value/variable "interface" is garbage collected when the function exits. I would suggest that you spend the time to learn basic classes as it will make keeping track of variables simpler.

from Tkinter import *
from functools import partial

class WLAN():
    def __init__(self):
        master = Tk()
        master.title('Wireless Warrior')
        self.interface = "initial value"  ## instance object=available through out the class

        self.label_interface = StringVar()
        self.label_interface.set(self.interface)
        w = Label(master, textvariable=self.label_interface, fg="blue", font=("Helvetica", 16))
        w.grid(row=0, column=0, columnspan=3)

        for but in range(1, 8):
            w = Button(master, text="Wlan"+str(but), width=4,
                       command=partial(self.wlan, but))
            w.grid(row=but,column=0)

        master.mainloop()

    def wlan(self, num):
        self.interface = "wlan" + str(num)
        print self.interface
        self.label_interface.set(self.interface)

WT=WLAN()
  • I used the print command to see if the variables would change when def Next was called. But it would default to wlan0 and 0 for power. My desired result is the command iwconfig wlanx txpower y and executes to set wireless interface and power level. By the way that is some really clean code, I have much to learn. – user2719869 Aug 27 '13 at 03:18
  • You don't have to jump through a bunch of hoops. In the example above the function "wlan" receives the number of the button pressed so you can do whatever for each number/button passed to the function, using an if statement or dictionary. – user2719958 Aug 27 '13 at 17:50