Python Forum
GUI and function not working together
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI and function not working together
#1
))
from tkinter import *
from tkinter import ttk 

def farcel(*args):
    try:
        vals = float(fahrenheit.get())
        celsius.set((vals - 32) * (5/9))
    except ValueError:
        pass

root = Tk()
root.title('Fahrenheit to Celsius')

frame = ttk.Frame(root, padding= "3 3 12 12")
frame.grid(column = 0, row = 0, sticky = (N, W, E, S))
frame.rowconfigure(0, weight = 1)
frame.columnconfigure(0, weight = 1)

fahrenheit = StringVar
celsius = StringVar

f_entry = ttk.Entry(frame, width = 7, textvariable = fahrenheit) 
f_entry.grid(column = 2, row = 1, sticky = (W, E))

c_label = ttk.Label(frame, textvariable = celsius)
c_label.grid(column = 2, row = 2, sticky = (W, E))

g_button = ttk.Button(frame, text = 'Go', command = farcel)
g_button.grid(column = 3, row = 4, sticky = W)

f_label = ttk.Label(frame, text = 'Degrees in d')
f_label.grid(column = 3, row = 2, sticky = W)

e_label = ttk.Label(frame, text = 'Equivalent to')
e_label.grid(column = 1, row = 2, sticky = W)

d_label = ttk.Label(frame, text = 'Degrees in c')
d_label.grid(column = 3, row = 3, sticky = W)

for child in frame.winfo.children():
    child.gridconfigure(padx = 5, pady = 5)

root.mainloop()
I am trying to set this up in GUI - this is my first time using this, so sorry for the basic questions! I am trying to create a program that a user will input a number they want to convert from fahrenheit to celsius and press go, and it will calculate it.
There is something wrong (with I think my function??) that is stopping this from working. I don't know what part is going wrong
It is saying that its getting a 'TypeError: get() missing 1 required positional argument: 'self' '

Also, if someone could explain 'frame.rowconfigure' and 'frame.columnconfigure' a little more to me because I don't really understand what it does.
Any guidance with this would be greatly appreciated :)
Reply
#2
You need to make a specific instance of StringVar. farenheit = StringVar assigns the general class StringVar to farenheit, not a specific instance of the class. You need farenheit = StringVar().
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jan-06-2019, 02:24 PM)ichabod801 Wrote: You need to make a specific instance of StringVar. farenheit = StringVar assigns the general class StringVar to farenheit, not a specific instance of the class. You need farenheit = StringVar().

Thank you! Such a silly thing I could figure out. All working smoothly now :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why this reverse lookup function not working Emekadavid 4 2,312 May-31-2020, 05:54 AM
Last Post: Emekadavid
  Function not working as intended I think? TimeForged 2 3,045 Mar-11-2018, 09:05 AM
Last Post: buran
  [split] Function not working as intended mihshyahoocom 1 2,101 Mar-11-2018, 09:04 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020