使用tkinter定位按钮时出现的问题

2024-04-29 15:34:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在tkinter中创建按钮时遇到问题,我尝试了不同的方法,但还没有找到解决方案

事实证明,当我制作这个计算器时,他们对我的要求之一是创建三个文本框和十一个按钮,所以当我创建框的位置时,我想放置按钮,但它们什么也不做,如果我试图告诉它们在第3列中定位,第4行就没有定位

Image of the code output

这是代码,如果您看到一些我无法看到的错误,而这正是导致此类错误的原因,我将不胜感激

from tkinter import *
#instancia de calculadora
calculadora = Tk()
#Nombre de la ventana grafica
calculadora.title("Practica calculadora con tkinter")
#tamano de la ventana
calculadora.geometry("600x750")
#color personalizado de la ventana
calculadora.configure(bg="black")

firtDisplay = Entry(calculadora, state="readonly", width=25).place(x=0, y=5)

secondDisplay = Entry(calculadora, state="readonly", width=25).place(x=300, y=5)

thirdDisplay = Entry(calculadora, state="readonly", width=25).place(x=149, y=40)

#Botones
Button(calculadora, text="7", width=15).grid(row=5, column=3)
Button(calculadora, text="8", width=15)
Button(calculadora, text="9", width=15)
 
calculadora.mainloop()

请帮忙会很好,谢谢你


Tags: text定位tkinter错误placedebuttonwidth
2条回答

不能在同一主窗口中混合使用.pack()、.grid()和.place()

我认为您必须首先只使用一种类型的几何体管理,然后使其保持最简单,我已经做了一些更改,以演示如何使用网格管理器和一些循环

 from tkinter import *
#instancia de calculadora
calculadora = Tk()
#Nombre de la ventana grafica
calculadora.title("Practica calculadora con tkinter")
#tamano de la ventana
#calculadora.geometry("600x750")
#color personalizado de la ventana
#calculadora.configure(bg="black")


r = 0
c = 0
for i in range(0,4):
    if i < 3:
        Entry(calculadora, state="readonly", width=15).grid(row=r, column=c)
    else:
        Checkbutton(calculadora,  width=15, text="/").grid(row=r, column=c)
    c +=1

array = (("7","8","9","x"),("4","5","6","-"),("1","2","3","+"))
r = 1
c = 0
for items in array:
    for i in items:
        index = items.index(i)
        if index < 3:
            Button(calculadora, text=i, width=15).grid(row=r, column=c)
        else:
            Checkbutton(calculadora,  width=15, text=i).grid(row=r, column=c)
        c +=1
    r +=1
    c = 0

calculadora.mainloop()

enter image description here

相关问题 更多 >