|
| 1 | +from tkinter import * |
| 2 | +#import tkinter as tk |
| 3 | + |
| 4 | +#window = tk.Tk() # this works |
| 5 | + |
| 6 | +# main window...All code goes in between these lines...window = Tk() and window.mainloop() |
| 7 | +window = Tk() # this works too. |
| 8 | + |
| 9 | +# function that converts km to mi... |
| 10 | +def km_to_miles(): |
| 11 | + #print(e1_value.get()) # the get() method of the StringVar object... |
| 12 | + miles=float(e1_value.get())*1.6 |
| 13 | + t1.insert(END,miles) # place this value after the last entry... |
| 14 | + |
| 15 | +# widgets (widgets go within the main window) |
| 16 | +b1=Button(window,text="Execute",command=km_to_miles) # parameters, separated by commas...You just pass the function name without the function() |
| 17 | +#b1.pack() |
| 18 | +# alternative way for .pack() |
| 19 | +b1.grid(row=0,column=0) |
| 20 | + |
| 21 | +e1_value=StringVar() # instantiate a StringVar object... |
| 22 | +e1=Entry(window,textvariable=e1_value) # e1_value for the user input... |
| 23 | +e1.grid(row=0,column=1) |
| 24 | + |
| 25 | +t1=Text(window,height=1,width=20) |
| 26 | +t1.grid(row=0,column=2) |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +window.mainloop() # all code goes before this line... |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +""" |
| 37 | +OUTPUT: |
| 38 | +miles=e1_value.get()*1.6 |
| 39 | +TypeError: can't multiply sequence by non-int of type 'float'...solution: miles=float(e1_value.get())*1.6...convert user input string to float... |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +""" |
| 44 | +Traceback (most recent call last): |
| 45 | + File "script1.py", line 2, in <module> |
| 46 | + import Tkinter as tk |
| 47 | +ModuleNotFoundError: No module named 'Tkinter' |
| 48 | +
|
| 49 | +for code interspection, do this: |
| 50 | +
|
| 51 | +invoke the python interactive shell... |
| 52 | + ipython |
| 53 | +
|
| 54 | +from tkinter import * |
| 55 | +
|
| 56 | +Button? |
| 57 | +""" |
0 commit comments