我决定尝试将Python和Tkinter作为第一个项目。单击相关按钮时,我遇到了更改变量的问题。到目前为止,我已经搜索了3天,但是为了执行我的程序中的'def Next'部分,我还没找到我需要的东西。对不起,我的代码很乱。任何有关编码的提示和快捷方式都表示赞赏。代码的最终结果应该执行,例如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()
答案 0 :(得分:0)
函数中的变量是函数的本地变量,因此当函数退出时,新的值/变量“interface”被垃圾收集。我建议你花时间学习基础课,因为它会使变量跟踪变得更简单。
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()