如何使我的编码计算器中的输入框居中?

我正在尝试使用 Python 3.7.2 在 Tkinter 中编写一个基本的计算器。没什么特别的,但我一直遇到同样的问题:看在耶稣的份上,我无法移动输入栏,使其位于所有输入按钮/第一行之上。我尝试将它放在不同的列中或使用其他版本的代码geometry,但绝对没有任何效果。我还在(德语)Q&A-Site Gutefrage上提出了问题,但似乎没有很多程序员/答案有点不清楚。关于如何做到这一点的任何想法?


这是我的代码(WIP):


from random import *

from math import *


def insert_1():

    text.insert(END, "1")

def insert_2():

    text.insert(END, "2")

def insert_3():

    text.insert(END, "3")

def insert_4():

    text.insert(END, "4")

def insert_5():

    text.insert(END, "5")

def insert_6():

    text.insert(END, "6")

def insert_7():

    text.insert(END, "7")

def insert_8():

    text.insert(END, "8")

def insert_9():

    text.insert(END, "9")

def insert_plus():

    text.insert(END, "+")

def insert_minus():

    text.insert(END, "-")

def insert_divide():

    text.insert(END, ":")

def insert_multiply():

    text.insert(END, "*")

def insert_ac():

    text.delete(0.0, END)

def insert_equals():

    text.delete(0.0, END)

    text.insert(END, "Calculating...")


慕容森
浏览 127回答 1
1回答

DIEA

您可以创建两个单独的框架并将一个放在另一个的顶部我创建了 fc 和 fc2 并将 fc 放在 fc2 之上from random import *from math import *from tkinter import *def insert_1():    text.insert(END, "1")def insert_2():    text.insert(END, "2")def insert_3():    text.insert(END, "3")def insert_4():    text.insert(END, "4")def insert_5():    text.insert(END, "5")def insert_6():    text.insert(END, "6")def insert_7():    text.insert(END, "7")def insert_8():    text.insert(END, "8")def insert_9():    text.insert(END, "9")def insert_plus():    text.insert(END, "+")def insert_minus():    text.insert(END, "-")def insert_divide():    text.insert(END, ":")def insert_multiply():    text.insert(END, "*")def insert_ac():    text.delete(0.0, END)def insert_equals():    text.delete(0.0, END)    text.insert(END, "Calculating...")calculator = Tk()fc = Frame(calculator)fc.grid(row=1)fc2  = Frame(calculator)fc2.grid(row = 2)text = Text(fc, width=20, height=1)bu1 = Button(fc2, text="1", activeforeground="white", activebackground="grey", command=insert_1)bu2 = Button(fc2, text="2", activeforeground="white", activebackground="grey", command=insert_2)bu3 = Button(fc2, text="3", activeforeground="white", activebackground="grey", command=insert_3)bu4 = Button(fc2, text="4", activeforeground="white", activebackground="grey", command=insert_4)bu5 = Button(fc2, text="5", activeforeground="white", activebackground="grey", command=insert_5)bu6 = Button(fc2, text="6", activeforeground="white", activebackground="grey", command=insert_6)bu7 = Button(fc2, text="7", activeforeground="white", activebackground="grey", command=insert_7)bu8 = Button(fc2, text="8", activeforeground="white", activebackground="grey", command=insert_8)bu9 = Button(fc2, text="9", activeforeground="white", activebackground="grey", command=insert_9)bu_ac = Button(fc2, text="AC", activeforeground="white", activebackground="grey", command=insert_ac)bu_plus = Button(fc2, text="+", activeforeground="white", activebackground="grey", command=insert_plus)bu_minus = Button(fc2, text="-", activeforeground="white", activebackground="grey", command=insert_minus)bu_divide = Button(fc2, text=":", activeforeground="white", activebackground="grey", command=insert_divide)bu_multiply = Button(fc2, text="x", activeforeground="white", activebackground="grey", command=insert_multiply)bu_equals = Button(fc2, text="=", activeforeground="white", activebackground="grey", command=insert_equals)text.grid(row = 1)bu1.grid(row=2, column=1)bu2.grid(row=2, column=2)bu3.grid(row=2, column=3)bu4.grid(row=3, column=1)bu5.grid(row=3, column=2)bu6.grid(row=3, column=3)bu7.grid(row=4, column=1)bu8.grid(row=4, column=2)bu9.grid(row=4, column=3)bu_plus.grid(row=2, column=4)bu_minus.grid(row=3, column=4)bu_divide.grid(row=4, column=4)bu_multiply.grid(row=2, column=5)bu_ac.grid(row=3, column=5)bu_equals.grid(row=4, column=5)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python