数据未包装在Tkinter Entry小部件中

时间:2019-09-15 17:32:24

标签: python python-3.x tkinter tkinter-canvas tkinter-layout

我有看起来像这样的数据。

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
    }
}

接下来,我将为该数据创建一个from ctypes import * mylib = CDLL('/home/tomdean/mylib.so') from mylib import ( mylib,p_mystruct ) struct_p = p_mystruct(None) rc = mylib.fills(struct_p) print(type(struct_p)) 。之后,我使用tempList2=[{'Date': '21-Aug-2019', 'Day': 'Sunday', 'Status': 'This is the message. It should be wrapped!!'}, {'Date': '22-Aug-2019', 'Day': 'Monday', 'Status': 'Message Delivered'}, {'Date': '23-Aug-2019', 'Day': 'Tuesday', 'Status': 'Invalid Data found!! Please retry'}] 模块查看数据。

这是示例代码:

dataframe

这是我得到的输出图像:

Output Image

在输出中,数据不会被包装。数据被破坏。我需要将数据包装在特定的行中。我有很多数据需要查看。因此,我需要滚动条才能访问它。为我提供一些包装数据的解决方案,以及一个附加到整个窗口的滚动条。

1 个答案:

答案 0 :(得分:0)

正如@stovfl所建议的那样,您可以使用tk.Text,它通过WORD关键字参数支持自动换行。

tk.Text(root, height=col_height, width=col_width, wrap=tk.WORD)

要处理的两个问题:

  1. 将一行中所有列的高度调整为相同。
    • 您可以在一行中找到最大的字符串,并据此确定列的高度。
  2. Enter键上,文本字段将光标向下移动,有时会隐藏文本。
    • 剥离文本并更新Enter键上的列。

enter image description here

代码

# ...

def change(event, row, col):
    # get value from Entry
    value = event.widget.get("1.0", tk.END)
    # update column & set value in dataframe
    event.widget.delete("1.0", tk.END)
    event.widget.insert("1.0", value.strip())
    df.iloc[row, col] = value.strip()
    print(df)


root = tk.Tk()

rows, cols = df.shape

col_width = 30

for r in range(rows):
    longest_string = max(df.loc[r].values, key=len)
    # If you know last element will be longest,
    # No need to calculate longest string
    # col_height = len(df.loc[r].values[-1])//col_width +1
    col_height = len(longest_string)//col_width + 1
    # or int(len(col_text)/col_width)+1
    for c in range(cols):
        col_text = df.iloc[r, c]
        e = tk.Text(root, height=col_height, width=col_width, wrap=tk.WORD)
        e.insert("1.0", df.iloc[r, c])
        e.grid(row=r, column=c)
        e.bind('<Return>', lambda event, y=r, x=c: change(event, y, x))
        e.bind('<KP_Enter>', lambda event, y=r, x=c: change(event, y, x))

# ...