1

i'm trying to make some sample GUI tkinter thats on one button try server status,and if it works,after click on button start it destroy gui and start some function that sends me file text. I've made gui,and server connection test button, but don't know how to destroy gui and start function.Thanks a lot :)

from tkinter import *
import requests, os

class form():
root = Tk()
wel = Label(root,text="Welcome")
serv = Entry(root,width=40)

def checkConn():
    if(requests.get(serv.get()).status_code==200):
        print("Succesfull")

def start(self):
    root.destroy()

prov = Button(root,text="Proveri",width=35, command = checkConn)
zap =  Button(root, text ="Zapocni",width=35,command =start)

wel.pack()
serv.pack()
prov.pack()
zap.pack()

root.mainloop()




form()

1 Answers1

1

I would recommend using an __init__() function for setting up the GUI as well as class variables. I rewrote your code in the way I would write it but without the requests bit, just to show GUI function.

from tkinter import *
import requests

class form():
    def __init__(self, master):
        self.master = master
        self.serv_text = StringVar()    # StringVar to hold entry value
        wel = Label(root, text="Welcome")
        serv = Entry(root, width=40, textvariable=self.serv_text)
        prov = Button(self.master, text="Proveri", width=35,
                      command=self.checkConn)
        zap =  Button(self.master, text ="Zapocni", width=35,
                      command=self.start)
        wel.pack()
        serv.pack()
        prov.pack()
        zap.pack()

    def checkConn(self):
        if(requests.get(self.serv_text.get()).status_code==200):
            print("Succesfull")
        else:
            print('miss')

    def start(self):
        self.master.destroy()

root = Tk()
form(root)
root.mainloop()

You will find more examples and discussion in Best way to structure a tkinter application.

figbeam
  • 7,001
  • 2
  • 12
  • 18
  • thanks a lot, but i still have smtg, when i try to use get method " def checkConn(self):print(self.serv.get())" it cause error AttributeError: 'form' object has no attribute 'serv' @figbeam – Djordje Milošević Dec 03 '18 at 01:08
  • You need to make "serv" an **instance** variable or the function will not find it. I have updated my example which now should work fine. – figbeam Dec 03 '18 at 05:48
  • Okay but when i make instance variable, they get() me every time empty string,cause i need to input string,and then do some function like check requests :) – Djordje Milošević Dec 03 '18 at 08:20
  • Ahh, I didn't quite understand you. I have updated my answer and now I use a StringVar to read from the entry and it works fine. However; if I enter an invalid URL I'll get an error. You will have to validate the entry or use a `try` clause to handle that. – figbeam Dec 03 '18 at 12:02
  • If the original question was answered you should open a new question instead of adding to the old. Also, I have never used pynput or logging so I'm not your man in that regard. – figbeam Dec 03 '18 at 19:31