简体   繁体   中英

tkinter button position throws "bad screen distance" error

Trying to assist a student in creating a calculator in Python but stuck on the positioning of buttons on a window using tkinter. Any help would be appreciated

I've created an array of button positions but getting a compile time error:

_tkinter.TclError: bad screen distance "25 25"

Code:

from tkinter import *

class GraphicsInterface:

    def __init__(self):
        self.window = Tk()
        self.window.geometry("720x500")

        self.clicked=[]

        numbers = [1,2,3,4,5,6,7,8,9,0]

        button_positions = [(0,25),(0,50),(0,100)]
        button_positions.append([(25,25),(25,50),(25,100)])
        button_positions.append([(50,25),(50,50),(50,100)])
        button_positions.append([(75,25)])

        i  = 0

        textbox = Text(self.window, width=30, height=2)
        textbox.pack()

        for n in numbers:
            button = Button(self.window, text=n, width=13)
            #button.place(x=25, y=100)
            button.pack()
            button.configure(command=lambda btn=button: self.OnClick(btn,textbox))
            position_t = button_positions[i]
            x = position_t[0]
            y = position_t[1]   

            button.place(x=x, y=y)
            i += 1

        self.window.mainloop()

    def OnClick(self, btn, textbox):
        text = btn.cget("text")
        print(text)
        textbox.insert('end', text)

app = GraphicsInterface()

change

button_positions.append([(25,25),(25,50),(25,100)])

to

button_positions.extend([(25,25),(25,50),(25,100)])

or just use "+"

Have you tried inheriting tk.Tk() in your GraphicsInterface class? You appear to be trying to create a list of button positions, but that's not what you're getting. Use button_positions.extend instead of button_positions.append .

I've also reorganized your code a bit to be more typical of a tkinter application.

import tkinter as tk  # avoid star imports if possible!
# prefix your widgets with 'tk.' instead, e.g.: tk.Button


class GraphicsInterface(tk.Tk):  # class inherits from tk.Tk()
    # REMOVE THIS: self.window = Tk()
    def __init__(self):
        self.geometry("720x500")  # set geometry
        self.clicked=[]

        numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

        # use 'extend' instead of 'append' to create a continuous list
        button_positions = [(0, 25), (0, 50), (0, 100)]
        button_positions.extend([(25, 25), (25, 50), (25, 100)])
        button_positions.extend([(50, 25), (50, 50), (50, 100)])
        button_positions.extend([(75, 25)])

        i  = 0

        # use 'self' as root instead of 'self.window'
        textbox = tk.Text(self, width=30, height=2)  
        textbox.pack()

        for n in numbers:
            # use 'self' as root instead of 'self.window'
            button = tk.Button(self, text=n, width=13)
            # you should use pack() -or- place() - you shouldn't need to
            # pack() here since you are place()ing below!
            # REMOVE THIS: button.pack()  
            button.configure(command=lambda btn=button: self.OnClick(btn,textbox))
            position_t = button_positions[i]
            x = position_t[0]
            y = position_t[1]   

            button.place(x=x, y=y)
            i += 1

        # REMOVE THIS: self.window.mainloop()

    def OnClick(self, btn, textbox):
        text = btn.cget("text")
        print(text)
        textbox.insert('end', text)


app = GraphicsInterface()
app.mainloop()  # call mainloop here instead

As a side note, since your UI is essentially a grid of buttons you should consider using the grid() geometry manager instead of pack() or place() - this might make things easier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM