Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

###################### 17.

2 Starting Tk and using Tkinter from Tkinter import * import sys win = Tk() button = Button(win, text = "Goodbye", command = sys.exit) button.pack() mainloop() ###################### 17.4 A simple Tkinter application from Tkinter import * # Initialize Tk and get a reference to the top-level window it automatically # creates. mainWindow = Tk() # Create a label which displays the text "Count: 0", and place it into the top-l evel window. countLabel = Label(mainWindow, text="Count: 0") countLabel.grid(row=0, column=1) # The value of the counter starts at 0. countValue = 0 # This function is called whenever the "Increment" button (defined below) is cli cked def incrementCount(): global countValue, countLabel # Increment the counter. countValue = countValue + 1 # Configure the counter label so that the text displayed in it is the string "Count: " followed by # the number in the counter. countLabel.configure(text = 'Count: ' + `countValue`) # Make a button displaying the text "Increment". Whenever the button is clicked, it executes as a # command the function 'incrementCount'. incrButton = Button(mainWindow, text="Increment", command=incrementCount) # Place the button into its parent window (which is "mainWindow"). incrButton.grid(row=0, column=0) # Make a button displaying the text "Quit":. Whenever it is clicked, it destroys the top level window. quitButton = Button(mainWindow, text="Quit", command=mainWindow.destroy) # Place the button into its parent window (which is "mainWindow"). quitButton.grid(row=1, column=0) # Enter the Tk event loop mainloop() ###################### 17.5 Creating widgets from Tkinter import * mainWindow = Tk() label = Label(mainWindow, text="Hello", background='white', foreground='red', fo nt='Times 20', relief='groove', borderwidth=3)

label.grid(row=0, column=0) mainloop() ###################### 17.6 Widget placement from Tkinter import * win = Tk() button1 = Button(win, text="one") button2 = Button(win, text="two") button1.grid(row=0, column=0) button2.grid(row=1, column=1) mainloop() #### from Tkinter import * main = Tk() # These commands ensure that any extra space given to the grid as a result of re sizing # the top-level window will be allocated to column 0, row 0, i.e. to the Text wi dget. main.columnconfigure(0, weight=1) main.rowconfigure(0, weight=1) text = Text(main) text.grid(row=0, column=0, sticky='nesw') verticalScroller = Scrollbar(main, orient='vertical') verticalScroller.grid(row=0, column=1, sticky='ns') horizontalScroller = Scrollbar(main, orient='horizontal') horizontalScroller.grid(row=1, column=0, sticky='ew') mainloop()

You might also like