#python #tkinter
#python #tkinter
Вопрос:
У меня немного странная проблема, и я попытаюсь объяснить.
Итак, я пытаюсь подключиться к Wi-Fi через tkinter, и я передаю переменные, такие как имя Wi-Fi и пароль, между классами и его работой. Я знаю это, потому что я отображаю их в ярлыках, но когда я пытаюсь подключиться к 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()
Комментарии:
1.
self.controller.a
иself.controller.passcode
естьStringVar
, поэтому нужно использоватьget()
для получения их содержимого:self.controller.a.get()
иself.controller.passcode.get()
.2. спасибо, @acw1669, я полностью пропустил эту часть xD