ภาพตัวอย่างสิ่งที่ต้องการจะทำ จากาพต้องการ Set ค่าให้กับตัว Edit Text เมื่อทำการ Run โปรแกรม ให้มีค่า ตามที่เรากำหนดเลย
เช่น Hostname : localhost
Database : Database
Username : User1
Password : *******
Web Driver Path : Your Path
โค้ดในส่วนของหน้าต่าง Login
โค้ด: เลือกทั้งหมด
class main():
def __init__(self,master):
self.master = master
self.username = StringVar()
self.password = StringVar()
self.hostname = StringVar()
self.database = StringVar()
self.pathwebdriver = StringVar()
self.widgets()
def selection(self):
root.filename = filedialog.askopenfile(initialdir = "/",title = "Select file",filetypes = (("files","*.exe"),("all files","*.*")))
fname = root.filename.name
self.pathwebdriver.set(fname)
def login(self):
link = self.hostname.get() + "/?db=" + self.database.get()
__builtin__.base_url = link
__builtin__.chromedriver = self.pathwebdriver.get()
__builtin__.username = self.username.get()
__builtin__.pwd = self.password.get()
__builtin__.db_name = self.database.get()
suite = unittest.TestLoader().loadTestsFromModule(alloy_all)
unittest.TextTestRunner(verbosity=2).run(suite)
def widgets(self):
self.head = Label(self.master,text = 'LOGIN',font = ('',25),pady = 10)
self.head.pack()
self.logf = Frame(self.master,padx =10,pady = 10)
Label(self.logf,text='Host Name', font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.hostname,bd = 5,font = ('',15)).grid(row=0,column=1)
Label(self.logf,text = 'Database ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.database,bd = 5,font = ('',15)).grid(row=1,column=1)
Label(self.logf,text='Username',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=2,column=1)
Label(self.logf,text = 'Password ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=3,column=1)
Label(self.logf,text = 'Path Webdriver ',font = ('',20),pady=5,padx=5 ).grid(sticky = W)
Entry(self.logf,textvariable = self.pathwebdriver,bd = 5,font = ('',15)).grid(row=4,column=1)
Button(self.logf,text = ' RUN ',bd = 3 ,font = ('',12),padx=5,pady=5, command=self.login).grid(row=7,column=1)
Button(self.logf,text = ' Browse ' ,bd = 3 ,font = ('',10),padx=5,pady=5, command=self.selection).grid(row=4,column=2)
Button(self.logf,text = ' Close ',bd = 3 ,font = ('',12),padx=5,pady=5, command=root.destroy).grid(row=7,column=2)
self.logf.pack()
if __name__ == '__main__':
root = Tk()
root.title('Alloy Login')
main(root)
root.mainloop()