Facebook
From Gentle Dormouse, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 187
  1. from Tkinter import *
  2. import tkFileDialog, tkMessagebox
  3.  
  4. class App:
  5.  
  6. def __init__(self, master):
  7.     self.master = master
  8.  
  9.     #call start to initialize to create the UI elemets
  10.     self.start()
  11.  
  12. def start(self):
  13.     self.master.title("This is the title of the 'Window'")
  14.  
  15.     self.now = datetime.datetime.now()
  16.  
  17.     #CREATE A TEXT/LABEL
  18.     #create a variable with text
  19.     label01 = "This is some text"
  20.     #put "label01" in "self.master" which is the window/frame
  21.     #then, put in the first row (row=0) and in the 2nd column (column=1), align it to "West"/"W"
  22.     Label(self.master, text=label01).grid(row=0, column=0, sticky=W)
  23.  
  24.     #CREATE A TEXTBOX
  25.     self.filelocation = Entry(self.master)
  26.     self.filelocation["width"] = 60
  27.     self.filelocation.focus_set()
  28.     self.filelocation.grid(row=1,column=0)
  29.  
  30.     #CREATE A BUTTON WITH "ASK TO OPEN A FILE"
  31.     self.open_file = Button(self.master, text="Browse...", command=self.browse_file) #see: def browse_file(self)
  32.     self.open_file.grid(row=1, column=1) #put it beside the filelocation textbox
  33.  
  34.     #CREATE RADIO BUTTONS
  35.     RADIO_BUTTON = [
  36.         ("This will display A", "A"),
  37.         ("This will display B","B")
  38.     ]
  39.  
  40.     #initialize a variable to store the selected value of the radio buttons
  41.     #set it to A by default
  42.     self.radio_var = StringVar()
  43.     self.radio_var.set("A")
  44.  
  45.     #create a loop to display the RADIO_BUTTON
  46.     i=0
  47.     for text, item in RADIO_BUTTON:
  48.         #setup each radio button. variable is set to the self.radio_var
  49.         #and the value is set to the "item" in the for loop
  50.         self.radio = Radiobutton(self.master, text=text, variable=radio_var, value=item)
  51.         self.radio.grid(row=2, column=i)
  52.         i += 1
  53.  
  54.     #now for a button
  55.     self.submit = Button(self.master, text="Execute!", command=self.start_processing, fg="red")
  56.     self.submit.grid(row=3, column=0)
  57.  
  58. def start_processing(self):
  59.     #more code here
  60.  
  61. def browse_file(self):
  62.     #put the result in self.filename
  63.     self.filename = tkFileDialog.askopenfilename(title="Open a file...")
  64.  
  65.     #this will set the text of the self.filelocation
  66.     self.filelocation.insert(0,self.filename)
  67. root = Tk()
  68. app = App(root)
  69. root.mainloop()