【问题标题】:Why In Python GUI Tkinder Grid system is not working properly for me?为什么在 Python GUI Tkinter Grid 系统对我来说不能正常工作?
【发布时间】:2020-10-18 15:46:56
【问题描述】:

下面的代码是我的python代码。我正在尝试用 GUI Tkinder 制作一个简单的计算器,但是这里的网格系统对我来说不能正常工作,请大家说是什么问题。

from Tkinter import *
from tkinter import font

window = Tk()
Screen = Label(window,
               text="Calculator",
               bg="#2ecc71",
               width=26,
               height=2,
               font=("Verdana", 22))

myFont = font.Font(size=12)


num7 = Button(window,
              text=7,
              height=5,
              width=10)
num7['font'] = myFont


num8 = Button(window,
              text=8,
              height=5,
              width=10)
num8['font'] = myFont

num9 = Button(window,
              text=9,
              height=5,
              width=10)
num9['font'] = myFont

window.geometry("500x500+420+120")
#window.resizable(False, False)     #commented for seeing the button that comes out of the boundary
window.title("Calculator -by Jobin")

Screen.grid(row=0, column=0, padx=10, pady=10)
num7.grid(row=1, column=0)
num8.grid(row=1, column=1)
num9.grid(row=1, column=2)

window.mainloop()

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:
    from tkinter import *
    from tkinter import font
    
    window = Tk()
    Screen = Label(window,
                   text="Calculator",
                   bg="#2ecc71",
                   width=26,
                   height=2,
                   font=("Verdana", 22))
    
    myFont = font.Font(family='Verdana', size=36, weight='bold')
    
    
    num7 = Button(window,
                  text=7)
    num7['font'] = myFont
    
    
    num8 = Button(window,
                  text=8)
    num8['font'] = myFont
    
    num9 = Button(window,
                  text=9)
    num9['font'] = myFont
    
    window.geometry("500x500+420+120")
    #window.resizable(False, False)     #commented for seeing the button that comes out of the boundary
    window.title("Calculator -by Jobin")
    
    Screen.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
    num7.grid(row=1, column=0)
    num8.grid(row=1, column=1)
    num9.grid(row=1, column=2)
    
    window.mainloop()
    

    添加了列跨度。更多信息请看这里: https://effbot.org/tkinterbook/grid.htm 这里是按钮和字体: How to change font and size of buttons and frame in tkinter using python?

    【讨论】:

    • 不客气。检查我发布的链接以及代码。我从 Effbot.org 学到了很多
    • 请问先生,我想多想一下,如何增加要填充按钮的字体文本?
    • 检查这是否回答了您的问题:stackoverflow.com/questions/20588417/…
    • @JobinS 更新了答案。 Button 小部件中的文本只是 Button 的一部分。所以如果你增加字体,它周围的一切都会适合它。因此,当您配置按钮的宽度和高度时,字体周围的空间是相同的,但里面的文本更大。我删除了宽度和高度的按钮配置。如果你愿意,你可以再添加一些。
    • "指定按钮的所需宽度。如果按钮中正在显示图像或位图,则该值以屏幕单位为单位(即 Tk_GetPixels 可接受的任何形式)。对于文本按钮(无图像或使用-compound none)然后宽度指定为文本标签分配多少字符空间。如果宽度为负数,则指定最小宽度。如果未指定此选项,则计算按钮的所需宽度从其中显示的图像或位图或文本的大小。” tcl.tk/man/tcl8.6/TkCmd/button.htm#M11
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多