如何仅更改 ttk.Entry 的一侧边框宽度? [英] How to change just one side border width of a ttk.Entry?

查看:21
本文介绍了如何仅更改 ttk.Entry 的一侧边框宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tkinter 网格和 ttk 条目显示数独布局(也许,我没有使用正确的方法来实现它).我想知道 ttk entry 的 style 选项是否有任何方法可以更改一侧的边框.我已经应用了这个功能:

I am trying to display a sudoku layout with tkinter grid and ttk entries (maybe, I am not using the right approach to achieve it). I would like to know if the style option of ttk entry has any way to change the border of just one side. I have applied this function:

def grid_layout(parent):
    
    s = ttk.Style()
    s.configure('border.TEntry',borderwidth=3)
    
    for row in range(1,10):
        for column in range(1,10):
            entry = None
            if column % 3 == 0 or row % 3 == 0:
                entry = ttk.Entry(parent, width=3,justify='center'
                                  ,style='border.TEntry')
            else:
                entry = ttk.Entry(parent,width=3,justify='center')
                
            entry.grid(column= column, row = row)

产生以下视图:

我只需要更改第 3-4、6-7 列和第 3-4 行和 6-7 行共享的边框宽度,作为典型的数独式布局.任何建议将不胜感激.

I just need to change the border width shared by colum 3-4, 6-7 and row 3-4 and 6-7 ,as the typical sudoku-like layout. Any recommendations would be appreciated.

推荐答案

正如我之前的评论中所述,我会使用堆叠布局,其中框位于框架中,条目位于这些框中.所以不需要特别的造型.访问 ttk.Entries 中的值给出了一个包含 tk.StringVar 对象的字典,键是大写字母 'A'..'I' 结合数字 '1'..'9' 就像在电子表格应用程序中一样:

As stated in my comment before, I'd use a stacked layout with the boxes in frames and the entries in these boxes. So there is no need for special styling. Access to the values within the ttk.Entries gives a dictionary that contains tk.StringVar objects, the keys are uppercase letters 'A'..'I' combined with numbers '1'..'9' like in spreadsheet applications:

import tkinter as tk
from tkinter import ttk

mainWin = tk.Tk()
mainWin.title('Sudoku solver')

mainFrame = ttk.Frame(mainWin, borderwidth=10)
mainFrame.grid(column=1, row=1)

# access to entries (of type tk.StringVar)
values = {}

for box_col in range(3):
    for box_row in range(3):
        box = ttk.Frame(mainFrame, borderwidth=1, relief='sunken')
        box.grid(column=box_col+1, row=box_row+1)
        for cell_col in range(3):
            for cell_row in range(3):
                v = tk.StringVar()
                # build spreadsheet key from overall column and row
                col = 'ABCDEFGHI'[3*box_col+cell_col]
                row = '123456789'[3*box_row+cell_row]
                key = col+row
                values[key] = v
                entry = ttk.Entry(box, width=3, justify='center', textvariable=v)
                entry.grid(column=cell_col+1, row=cell_row+1)

# example for accessing the entries
values['A1'].set(1)
values['G3'].set(7)
values['E5'].set(int(values['A1'].get())+4)
values['I9'].set(int(values['E5'].get())+4)

mainWin.mainloop()

在 Windows 8.1 下,这看起来像这样:

Under Windows 8.1, this will look this:

这篇关于如何仅更改 ttk.Entry 的一侧边框宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆