Python place in frames

84 Views Asked by At

I have been trying to create a 'dice simulator' using Tkinter in python, but when i try to poisition my 'Text' widget inside its Frame, it stays fixed in the top left of the frame. I have tried the grid, move, and place functions but none work. Here is my code:

from tkinter import *
from random import *
def roll():
    txt.delete(0.0, END)
    txt.insert(END, str(randint(1,6)))
window = Tk()
window.title('Dice simulator')
canvas = Canvas(window, width=800, height=500, bg='brown')
canvas.pack()
dice = canvas.create_rectangle(375, 225, 425, 275, fill='white')
frame = Frame(canvas, width=25, height=15)
window = canvas.create_window(400, 295, window=frame)
txtframe = Frame(canvas, width=50, height=50)
txtwindow = canvas.create_window(400, 250, window=txtframe, width=49, height=49)
txt = Text(txtframe, font=50)                                                                                                                                         
button = Button(frame, text='Roll', command=roll)
button.pack()
txt.pack()

What are your suggestions? Help will be appreciated.

Details given above, not amazing at this website. Just started.

2

There are 2 best solutions below

2
figbeam On

Centering content in a frame can be done with the pack parameters expand and fill:

widget.pack(expand=True, fill='both')

To keep the frame from adjusting its size to the contents you can use the pack_propatage method.

widget.pack_propagate(False)

I rewrote your code using a Label instead of a Text widget, and rearranged it for better readability:

from tkinter import *
from random import *

def roll():
    dice_txt.config(text=str(randint(1,6)))

window = Tk()
window.title('Dice simulator')
canvas = Canvas(window, width=800, height=500, bg='brown')
canvas.pack()

# Dice
dice = canvas.create_rectangle(375, 225, 425, 275, fill='white')
txtframe = Frame(canvas, width=50, height=50)
# Keep txtframe from changing size when dice_txt does
txtframe.pack_propagate(False)
dice_txt = Label(txtframe, font=50)
# Put the dice_text in te center of txtframe
dice_txt.pack(expand=True, fill='both')
txtwindow = canvas.create_window(400, 250, window=txtframe, width=49, height=49)

# Roll button
frame = Frame(canvas, width=25, height=15)
window = canvas.create_window(400, 295, window=frame)
button = Button(frame, text='Roll', command=roll)
button.pack()
0
acw1668 On

For your simple dice simulator, you don't need those frame and text widgets, just use .create_text() to show the generated dice number instead:

import random
import tkinter as tk

def roll():
    canvas.itemconfig(dice, text=random.randint(1, 6))

window = tk.Tk()
window.title("Dice Simulator")
canvas = tk.Canvas(window, width=800, height=500, bg="brown")
canvas.pack()
canvas.create_rectangle(375, 225, 425, 275, fill="white")
dice = canvas.create_text(400, 250, font=('', 40))
button = tk.Button(canvas, text="Roll", width=5, command=roll)
canvas.create_window(400, 295, window=button)
window.mainloop()

Result:

enter image description here