0

Current Application3

What i've circled is what I can't move even though I have the side as right and not bottom. 1

This is the label in the code 2

I've got a pretty simple python code for a weight converter, and I want to attach the answer to the frame so it will be below the entry box. I won't attach there but will only attach to either the top or bottom. Same thing goes for the buttons, I can attach anything anywhere other than the top or the bottom. Sorry if some of this doesn't make sense, I am very new to python.

import tkinter as tk
from tkinter.constants import BOTTOM
from typing import Text

root = tk.Tk()

root.resizable(False, False)

root.title('Weight Converter') 

canvas = tk.Canvas(root, height=300, width=300, bg='teal')

frame=tk.Frame(root)
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)

labelanswer=tk.Label(frame, bg='white')
labelanswer.place(x=105, y=90)

def to_kg():
    pound = float(entry1.get())*2.20462
    labelanswer['text'] = pound 

def from_kg():
    kg=float(entry1.get())/2.20462
    labelanswer['text'] = kg

entry1 = tk.Entry (root) 
canvas.create_window(200, 200, window=entry1)
entry1.place(x=95, y=90)

labeltext=tk.Label(root, text="Weight Converter", bg='white')
labeltext.pack()

b1 = tk.Button(root, text="to kg", bg='white', command=to_kg)

b2=tk.Button(root, text='from kg', command=from_kg, bg='white') 

labelanswer.pack()
b2.pack(side=BOTTOM)
b1.pack(side=BOTTOM)
canvas.pack()
frame.pack()

root.mainloop()
Matiiss
  • 5,970
  • 2
  • 12
  • 29
R Errett
  • 3
  • 3
  • Can you provide screenshot of your actual application and sketch of desired effect? – Daweo Nov 29 '21 at 19:37
  • @Daweo Just added it – R Errett Nov 29 '21 at 19:47
  • every widgets first parameter is root, therfore root becomes the master. Is this the issue you are looking for? – Thingamabobs Nov 29 '21 at 19:51
  • I understand that, But the more I try I can't get it to attach to anything else but the root. I want to attach it right next to the entry box, which I can't get it to do, I've given it cords, I've given it a different frame. If I attach it to anything all it does is attach to the frame. Check the screen shot to see what I mean. – R Errett Nov 29 '21 at 19:55
  • You need to make frame the parent of the widget, not root – jezza_99 Nov 29 '21 at 19:56
  • 1
    You may find [this helpful](https://stackoverflow.com/a/63536506/13629335) – Thingamabobs Nov 29 '21 at 19:59
  • `place` is rarely used, it is better to use `pack` and/or `grid` as they also adjust for resizing, anyways, I don't particularly understand what problem you are facing tho, the image doesn't help much nor does the explanation. If you want to show a visual then you could also draw some boxes on the image to show how it is also supposed to look (perhaps two images, one with current and one that shows the expected). – Matiiss Nov 29 '21 at 20:19
  • @Matiiss I added new screen shots maybe it will help you better understand. I just can't move it no matter if I put root, canvas, add the side in pack, add it to the grid, add a a x/y cord or frame. Nothing will move it. – R Errett Nov 29 '21 at 21:53

1 Answers1

1

You would have to do the same thing to the buttons and labelAnswer that you did for the entry.

canvas.create_window(window=YOUR WIDGET HERE)

However, you can remove the canvas all together by using:

root.geometry('300x300')
root.config(bg = 'teal')

geometry() takes a string 'WIDTH x HEIGHT' and will set the widget accordingly.

config() will allow you to change attributes of most widgets after they are created. In the case above we chang the background color (bg) to teal

This allows you to remove all of the canvas fluff and make code more readable

You can also use the bg option at object creation too.

labelText = tk.Label(root, text = "Weight Converter", bg = 'teal')

You can use strings instead of built-in constants too, weither or not is the best option I'll leave to the community. But I would suggest either tk.BOTTOM or 'bottom' over just importing the constant as it just adds fluff.

I went through and cleaned some things up, but this is what I ended up with:

import tkinter as tk

def to_kg():
    pound = float(entry.get())*2.20462
    labelAnswer['text'] = pound 

def to_pounds():
    kg=float(entry.get())/2.20462
    labelAnswer['text'] = kg

# Creating root window
root = tk.Tk()

# geometery() allows you to set the width and hieght of a window with a string 'WIDTH x HIEGHT'
root.geometry('300x300')
root.resizable(False, False)

# There are many things config can do, here we set the backround color to teal removing the need of a canvas
root.config(bg = 'teal')
root.title('Weight Converter')

# We can also set the color of the lables using the bg option
labelText = tk.Label(root, text = "Weight Converter", bg = 'teal')
    
# We use pack to 'place' the widget so it shows up 
labelText.pack()

# Now that we are not using the canvas we can pack the Entry widget 
entry = tk.Entry(root) 
entry.pack()

# Creating and placing the answer label
labelAnswer = tk.Label(root)
labelAnswer.pack()

# Our buttons that will call our conversion functions
b1 = tk.Button(root, text="to kg", command = to_kg)
b1.pack(side = 'bottom')

b2 = tk.Button(root, text='to pounds', command = to_pounds) 
b2.pack(side = 'bottom')

root.mainloop()