Python Tkinter变量值未传递给其他类

Python Tkinter变量值未传递给其他类,python,tkinter,Python,Tkinter,我有一点奇怪的问题,我会尽力解释 所以我试图通过tkinter连接到WiFi,我在类之间传递WiFi名称和密码等变量,以及它们的工作方式。我知道这一点,因为我在标签中显示它们,但当我尝试连接到WiFi时,变量不会作为参数传递给连接字符串 以下是我的部分代码: import tkinter as tk from PIL import Image, ImageTk import time import os from wifi import Cell, Scheme LARGE_FONT

我有一点奇怪的问题,我会尽力解释

所以我试图通过tkinter连接到WiFi,我在类之间传递WiFi名称和密码等变量,以及它们的工作方式。我知道这一点,因为我在标签中显示它们,但当我尝试连接到WiFi时,变量不会作为参数传递给连接字符串

以下是我的部分代码:

 import tkinter as tk
 from PIL import Image, ImageTk
 import time
 import os
 from wifi import Cell, Scheme
 LARGE_FONT = ("Verdana", 12)


 from tkinter import END




class SeaofBTCapp(tk.Tk):

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    container = tk.Frame(self)
    self.geometry("480x800")
    self.label = tk.Label(text="This is the start page",background="blue",height=2)
    self.label.pack(side="top", fill="x", pady=0)
    self.time1 = ''
    self.time2 = time.strftime('%H:%M:%S')
    self.watch = tk.Label(self, text=self.time2, font=('times', 12, 'bold'))
    self.watch.pack()
    self.changeLabel() #first call it manually

    
    self.a=tk.StringVar()  
    self.passcode=tk.StringVar()





    container.pack(side="top", fill="both", expand=True)

    container.grid_rowconfigure(0, weight=100)
    container.grid_columnconfigure(0, weight=1)

    self.frames = {}

    for F in (SetUpPage,ChoseWIFI,TypePassword,Connecting):
        frame = F(container, self)

        self.frames[F] = frame

        frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame(SetUpPage)

def show_frame(self, cont):
    frame = self.frames[cont]
    frame.tkraise()

def changeLabel(self):
    self.time2 = time.strftime('%H:%M:%S')
    self.watch.configure(text=self.time2)
    self.after(200, self.changeLabel)  # it'll call itself continuously











class SetUpPage(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    image = Image.open("wifi_icon.gif")
    image = image.resize((50, 50), Image.ANTIALIAS)

    self.reset_img = ImageTk.PhotoImage(image)

    image1 = Image.open("ethernet_icon.gif")
    image1 = image1.resize((50, 50), Image.ANTIALIAS)

    self.reset_img1 = ImageTk.PhotoImage(image1)
    self.but = tk.Button(self, text="WORK IN", height=180, width=180, image=self.reset_img,command=lambda: controller.show_frame(ChoseWIFI)).place(x=150, y=125)
    self.but1 = tk.Button(self, text="WORK OUT", height=180, width=180, image=self.reset_img1).place(x=150, y=425)

class ChoseWIFI(tk.Frame):



def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller=controller

    
    self.mylistbox=tk.Listbox(self,width=80,height=10,font=('times',13))
    self.mylistbox.bind('<<ListboxSelect>>',self.CurSelet)
    self.mylistbox.place(x=100,y=190)
    self.wifiscan()
    self.but = tk.Button(self, text="CONNECT", height=10, width=10,command=lambda: controller.show_frame(TypePassword)).place(x=150, y=525)

    


    
def wifiscan(self):

   allSSID = [cell.ssid for cell in Cell.all('wlan0')]
   print (allSSID )# prints all available WIFI SSIDs
  



   for i in range(len(allSSID )):
        if print(str(allSSID [i]))== print(myssid):
            a = i
            print("hit")
            myssidA = allSSID [a]
            print( myssidA )
            break
        else:
            print ("getout")



   print (myssid)

   
   self.itemsforlistbox=allSSID
   
   for items in self.itemsforlistbox:
       self.mylistbox.insert(END,items)

def CurSelet(self,a):
    value=self.mylistbox.get(self.mylistbox.curselection())
   
    self.o=self.controller.a
    self.o.set(value)
    

    

    
class TypePassword(tk.Frame):



def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller=controller
    #self.entry1 = tk.Label(self, textvariable=self.controller.shared_data["username"])
    #self.entry1.pack()
    
    self.L2 = tk.Label(self, textvariable=self.controller.a)
    self.L2.pack()
    self.L1 = tk.Label(self, text="Type password")
    self.L1.pack()
    
    
    
    self.E1 = tk.Entry(self, bd=5)
    self.E1.place(x=150, y=325)
    #self.connect()
   
    
    
    self.but = tk.Button(self, text="CONNECT", height=10, width=10,command=lambda:[self.connect(), controller.show_frame(Connecting)]).place(x=150, y=525)
def connect(self):
    self.controller.passcode.set(self.E1.get())
    #os.system('nmcli d wifi connect "%s" password %s iface %s' % (self.controller.a,self.controller.passcode, self.controller.a)) # vive1234 is the password to my wifi myssidA is the wifi name 

        
    self.pas=self.controller.passcode
    self.passw = self.E1.get()
    
   
    self.pas.set(self.passw)
    

class Connecting(tk.Frame):



def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller=controller
    
    self.entry1 = tk.Label(self, text="CONNECTING.....")
    self.entry1.pack()
    
    self.entry2 = tk.Label(self, textvariable=self.controller.a)
    self.entry2.pack()
    
    self.entry3 = tk.Label(self, textvariable=self.controller.passcode)
    self.entry3.pack()
   
    
    self.but = tk.Button(self, text="CONNECT11", height=10, width=10,command=self.k).place(x=150, y=525)
    #self.but.tk.bind(('<but>', self.k))
     
def k(self):
    #connection string for WIFI
    os.system('nmcli d wifi connect "%s" password %s iface %s' % (self.controller.a,self.controller.passcode, self.controller.a)) # vi
    
    print("KKKK")

if __name__ == "__main__":

# Calls its attribute.

app = SeaofBTCapp()
a=app.changeLabel()

app.after(200,a )
# Calls its attribute.


app.mainloop()
将tkinter作为tk导入
从PIL导入图像,ImageTk
导入时间
导入操作系统
从wifi导入小区,方案
大字体=(“Verdana”,12)
从tkinter导入端
STCAP(tk.tk)的等级:
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
容器=tk.框架(自身)
自几何(“480x800”)
self.label=tk.label(text=“这是起始页”,background=“蓝色”,height=2)
self.label.pack(side=“top”,fill=“x”,pady=0)
self.time1=''
self.time2=time.strftime(“%H:%M:%S”)
self.watch=tk.Label(self,text=self.time2,font=('times',12,'bold'))
self.watch.pack()
self.changelab()#首先手动调用它
self.a=tk.StringVar()
self.passcode=tk.StringVar()
container.pack(side=“top”,fill=“both”,expand=True)
container.grid_rowconfigure(0,权重=100)
container.grid\u column配置(0,权重=1)
self.frames={}
对于F in(设置页面,ChoseWIFI,键入密码,连接):
框架=F(容器,自身)
self.frames[F]=帧
frame.grid(行=0,列=0,sticky=“nsew”)
自我显示框架(设置页面)
def显示画面(自身,续):
帧=自身帧[续]
frame.tkraise()
def更改标签(自我):
self.time2=time.strftime(“%H:%M:%S”)
self.watch.configure(text=self.time2)
self.after(200,self.changelab)#它会不断地调用自己
类设置页(tk.Frame):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
image=image.open(“wifi\u icon.gif”)
image=image.resize((50,50),image.ANTIALIAS)
self.reset\u img=ImageTk.PhotoImage(图像)
image1=Image.open(“ethernet\u icon.gif”)
image1=image1.resize((50,50),Image.ANTIALIAS)
self.reset_img1=ImageTk.PhotoImage(image1)
self.but=tk.按钮(self,text=“磨合”,高度=180,宽度=180,图像=self.reset\u img,命令=lambda:controller.show\u frame(ChoseWIFI))。放置(x=150,y=125)
self.but1=tk.按钮(self,text=“OUT”,高度=180,宽度=180,图像=self.reset\u img1)。放置(x=150,y=425)
ChoseWIFI类(传统框架):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
self.mylistbox=tk.Listbox(self,宽度=80,高度=10,字体=('times',13))
self.mylistbox.bind(“”,self.CurSelet)
self.mylistbox.place(x=100,y=190)
self.wifiscan()
self.but=tk.Button(self,text=“CONNECT”,高度=10,宽度=10,命令=lambda:controller.show_frame(TypePassword)).place(x=150,y=525)
def wifiscan(自):
allSSID=[cell.sall中的cell的cell.ssid('wlan0')]
打印(allSSID)#打印所有可用的WIFI SSID
对于范围内的i(len(allSSID)):
如果print(str(allSSID[i])==print(myssid):
a=i
打印(“点击”)
myssidA=allSSID[a]
打印(myssidA)
打破
其他:
打印(“外出”)
打印(myssid)
self.itemsforlistbox=allSSID
对于self.itemsforlistbox中的项目:
self.mylistbox.insert(结束,项目)
def游标(自身,a):
value=self.mylistbox.get(self.mylistbox.curselection())
self.o=self.controller.a
self.o.set(值)
类类型密码(tk.Frame):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
#self.entry1=tk.Label(self,textvariable=self.controller.shared_data[“username”])
#self.entry1.pack()
self.L2=tk.Label(self,textvariable=self.controller.a)
self.L2.pack()
self.L1=tk.Label(self,text=“键入密码”)
self.L1.pack()
self.E1=tk.Entry(self,bd=5)
自身E1位置(x=150,y=325)
#self.connect()
self.but=tk.Button(self,text=“CONNECT”,高度=10,宽度=10,命令=lambda:[self.CONNECT(),controller.show_frame(Connecting)]).place(x=150,y=525)
def连接(自):
self.controller.passcode.set(self.E1.get())
#os.system('nmcli d wifi连接“%s”密码%s如果是%s%%(self.controller.a,self.controller.passcode,self.controller.a))#vive1234是我的wifi的密码myssidA是wifi名称
self.pas=self.controller.passcode
self.passw=self.E1.get()
self.pas.set(self.passw)
类连接(tk.框架):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
self.entry1=tk.Label(self,text=“CONNECTING…”)
self.entry1.pack()
self.entry2=tk.Label(self,textvariable=self.controller.a)
self.entry2.pack()
self.entry3=tk.Label(self,textvariable=self.controller.passcode)
self.entry3.pack()
self.but=tk.按钮(self,text=“CONNECT11”,高度=10,宽度=10,命令=self.k)。放置(x=150,y=525)
#self.but.tk.bind(('',self.k))
def k(自我):
#WIFI连接字符串
操作系统('nmcli d wifi连接“%s”密码%s如果是%s%%(self.controller.a,self.controller.passcode,self.controller.a))#vi
印刷品(“KKK”)
如果名称=“\uuuuu main\uuuuuuuu”:
#调用其属性。
app=seaofbtcap()
a=app.changelab()
附录.之后(200,a)
#调用其属性。
app.mainloop()

self.controller.a
self.controller.passcode
都是
StringVar
,所以需要使用
get()
来获取它们的内容:
self.controller.a.get()
self.controller.passcode.get()
。谢谢你@acw1669我完全错过了那部分