Advertisement
cmiN

gui test

Dec 25th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  # GUI v1.0
  3.  # 25.12.2011 cmiN
  4.  #
  5.  # This is just a simple app for my future project.
  6.  
  7.  
  8. from Tkinter import *
  9. import urllib
  10.  
  11.  
  12. class GUI(Frame):
  13.     """
  14.    Main GUI class inherited from Frame.
  15.    Like this, we create our main frame with all widgets on it.
  16.    """
  17.    
  18.     def __init__(self, master=None, width=300, height=300):
  19.         Frame.__init__(self, master, width=width, height=height)
  20.         self.grid()
  21.         self.create_widgets()
  22.         self.set_behavior()
  23.        
  24.     def create_widgets(self):
  25.         """ Here are born our lovely widgets. """
  26.         self.first = IntVar()
  27.         self.second = IntVar()
  28.         self.select = IntVar()
  29.         self.menuButton = Menubutton(self, text="New")
  30.         self.menuButton.menu = Menu(self.menuButton, tearoff=0)
  31.         self.label = Label(self, text="Welcome!", fg="orange")
  32.         self.label2 = Label(self, text="Link:")
  33.         self.scrollbar = Scrollbar(self, orient="horizontal", command=self.__scroll_handler)
  34.         self.entry = Entry(self, width=20, fg="grey", xscrollcommand=self.scrollbar.set)
  35.         self.button = Button(self, text="Quit", command=self.quit, width=5)
  36.         self.button2 = Button(self, text="Get", command=self.get_url, width=5)
  37.         self.menuButton.grid()
  38.         self.label.grid(row=1, column=0, pady=10, columnspan=3)
  39.         self.label2.grid(row=2, column=0)
  40.         self.entry.grid(row=2, column=1, columnspan=2, padx=10)
  41.         self.scrollbar.grid(row=3, column=1, columnspan=2, padx=10, sticky=W+E)
  42.         self.button2.grid(row=4, column=1, sticky="se", pady=5, columnspan=1)
  43.         self.button.grid(row=4, column=2, pady=5, sticky="sw", columnspan=1)
  44.         self.label2.grid_forget()
  45.        
  46.     def set_behavior(self):
  47.         self.menuButton["menu"] = self.menuButton.menu
  48.         self.menuButton.menu.add("checkbutton", label="First", variable=self.first, command=self.show)
  49.         self.menuButton.menu.add("checkbutton", label="Second", variable=self.second)
  50.         self.menuButton.menu.add("radiobutton", label="a", variable=self.select, value=0)
  51.         self.menuButton.menu.add("radiobutton", label="b", variable=self.select, value=1)
  52.         self.menuButton.menu.add("radiobutton", label="c", variable=self.select, value=2)
  53.         self.entry.insert(0, "Enter link...")
  54.         self.entry.bind("<Button-1>", self.__event_handler)
  55.         self.second.set(1)
  56.        
  57.     def show(self):
  58.         print self.select.get()
  59.        
  60.     def get_url(self):
  61.         fout = open("index.html", "wt")
  62.         sock = urllib.urlopen(self.entry.get())
  63.         fout.write(sock.read())
  64.         fout.close()
  65.        
  66.     def __scroll_handler(self, *args):
  67.         if args[0] == "scroll":
  68.             self.entry.xview_scroll(args[1], args[2])
  69.         elif args[0] == "moveto":
  70.             self.entry.xview_moveto(args[1])
  71.        
  72.     def __event_handler(self, event):
  73.         if self.entry["fg"] == "grey":
  74.             self.entry.delete(0, END)
  75.             self.entry["fg"] = "black"
  76.  
  77.  
  78. def main():
  79.     app = GUI()
  80.     app.master.title("GUI v1.0")
  81.     #app.grid_propagate(False)
  82.     app.mainloop()
  83.    
  84.    
  85. if __name__ == "__main__":
  86.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement