Tkinter and class

Asked

Viewed 45 times

-1

I am beginner in classes and Tkinter and am having a problem that I think is by little knowledge of me in POO. In the code below, only one label is running, neither buttons nor anything execute, what can it be? I gave a smaller list to fit aq in the question. I had some errors of identation when posting the code aq, I’m sorry.

from tkinter import *
import tkinter as tk
from tkinter import ttk

currencys_id = ['AED', 'AFN', 'ALL', 'AMD']

class CurrencyConverter(tk.Frame):
def __init__(self, master):
    super().__init__(master)
    self.master = master
    self.font = ('Courier',15,'bold')
    
    # Labels
    self.subtitle = tk.Label(self, text='Welcome to Real Time Currency Converter', font=self.font, fg='#ffffff', bg='#245985')
    self.toLabel = tk.Label(self, text='TO', font=self.font)
    
    # Combobox
    self.toCurrency = ttk.Combobox(self, values=currencys_id, font=self.font, width=10, justify='center', state="readonly")
    self.toCurrency.set('BRL')
    #self.toCurrency.bind('<<ComboboxSelected>>', currencyToConvert)
    
    self.fromCurrency = ttk.Combobox(self, values=currencys_id, font=self.font, width=10, justify='center', state="readonly")
    #self.fromCurrency.bind('<<ComboboxSelected>>', currencyConverted)
    
    # Entrys
    self.inputValue = tk.Entry(self, font=self.font, width=8, justify='center')
    self.inputValue.insert(0, '1')
    
    self.outputValue = tk.Entry(self, font=self.font, width=8, justify='center')
    
    # Buttom
    self.convertButtom = tk.Button(self, text='Convert', font=self.font, width=8, bg='#097FDF', fg='#ffffff')
    
    # Pack()
    self.pack()
    self.subtitle.pack()
    self.toLabel.pack()
    self.toCurrency.pack()
    self.fromCurrency.pack()
    self.inputValue.pack()
    self.outputValue.pack()
    self.convertButtom.pack()
    
    # position
    self.toLabel.place(x=230, y=100)
    self.toCurrency.place(x=50, y=100)
    self.fromCurrency.place(x=300, y=100)
    self.inputValue.place(x=70, y=140)
    self.outputValue.place(x=320, y=140)
    self.convertButtom.place(x=195, y=160)
    
    #def clickButtom(self):
        
def main(): 
root = tk.Tk()
app = CurrencyConverter(master=root)
root.title('Currency Converter')
root.maxsize(500, 200)
app.mainloop()

if __name__ == '__main__':
    main()
    

2 answers

0

The problem is in the use of two layout managers that is being used in the script pack is one, and place is another, the same object should not use both at the same time, at the moment do not remember the correct way to use place, if vc comment this part the layout made with pack will work


I did a quick test with the place, and the frame size really influences

from tkinter import *

r = Tk()
r.minsize(200,200)

f = Frame(r, width=100, height=100, bg='#aabbcc')
f.pack()

l1 = Label(f, text='teste1')
l2 = Label(f, text='teste2')

l1.place(x=0, y=0)
l2.place(x=90, y=90)

r.mainloop()

when running the code you can see that the label L2 is cut. This shows that in your code you must specify a size for the frame

  • Thank you so much, you’ve really helped me spot the error!

  • @lucas_boscatti try to start with something simple (just make a frame with a button) to intender how the place works, think the problem is in the frame size, try to put a fixed size for the frame, and then position some widget in x=0 y=0, and then go doing test type: x=10 y=10.

0

As Elton Nunes commented, to "disable" the place of your code it already shows changes. as in the following images: mantendo o 'position'

inutilizando o 'position'

But if your intention is just that, despite being a bad solution, it works. However if you are thinking about the functionality of your converter, wondering why it is not working, it is because your code is still very superficial, there are no event assignments for the click, for example:

import tkinter as tk
from tkinter import ttk, messagebox

[...]

# Buttom
        def nova_caixa():
            messagebox.showinfo('Olá', 'Eu sou o Goku!')

        self.convertButtom = tk.Button(self, text='Convert', font=self.font, width=8, bg='#097FDF', fg='#ffffff',
                                       command=nova_caixa)

evento criado ao clicar no botão

I hope that somehow that might have cleared something up.

  • So, I understood this part, but now I’m not getting the positions of my widgets. I’m still only doing the visual part of the project, without the functions of conversions and clicks. How now can I reposition my widgets? thanks for the contribution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.