如何在旋转框中逐行索引,而不是按字符编号?

时间:2017-01-15 16:47:29

标签: python regex python-3.x indexing tkinter

我正在尝试使用单个文本文件(file.txt)填充具有不同值的2个旋转框。我通过在文件中搜索“横幅”(TITLE)并使用2个给定横幅之间的值填充每个横幅来管理它。

'file.txt'将是这样的:

(没有额外的换行符)

............ #exactly 12与代码中的模式相匹配

第1标题

............

第一件事

第二件事

............

TITLE 2

............

次级

............

TITLE 3

............

from tkinter import *
import tkinter as tk
import re

root = Tk()

class Main():

    def __init__(self, root):
        pass

    def Thing1():
        read_thing = open('file.txt', 'r')
        read_names = read_thing.read()
        print (read_names)
        pattern3 =r'^(............)$\
    ^(TITLE 1)$\
    ^(............)$'
        pattern4 = r'^(............)$\
    ^(TITLE 2)$\
    ^(............)$'
        multiline3 = re.compile(pattern3, re.MULTILINE)
        multiline4 = re.compile(pattern4, re.MULTILINE)
        for match in multiline3.finditer(read_names):
            s=match.start()
            e=match.end()
        for match in multiline4.finditer(read_names):
            s1=match.start()
            e1=match.end()
            output = (read_names[e: s1])
            print (output)
            return output

    def Thing2():
        read_thing = open('file.txt', 'r')
        read_names = read_thing.read()
        print (read_names)
        pattern4 =r'^(............)$\
    ^(TITLE 2)$\
    ^(............)$'
        pattern5 = r'^(............)$\
    ^(TITLE 3)$\
    ^(............)$'
        multiline4 = re.compile(pattern4, re.MULTILINE)
        multiline5 = re.compile(pattern5, re.MULTILINE)
        for match in multiline4.finditer(read_names):
            s=match.start()
            e=match.end()
        for match in multiline5.finditer(read_names):
            s1=match.start()
            e1=match.end()
            output = (read_names[e: s1])
            print (output)
            return output

    values1 = Main.Thing1()
    values2 = Main.Thing2()

    def sb1_changed():
        selected = var1.get()
        idx = values1.index(selected)
        print('selected:', selected)
        print('index', idx)
        var2.set(values2[idx])

    def sb2_changed():
        selected = var2.get()
        idx = values2.index(selected)
        print('selected:', selected)
        print('index', idx)
        var1.set(values1[idx])

var1 = tk.StringVar()
var2 = tk.StringVar()

spinbox1=tk.Spinbox(self, textvariable=var1, values=values1,
                                   command=sb1_changed)
spinbox1.pack()
spinbox2=tk.Spinbox(self, textvariable=var2, values=values2,
                                   command=sb2_changed)
spinbox2.pack()

root.mainloop()

使用sb1_changed和sb2_changed的想法是,当我滚动浏览并因此在1个spinbox中选择不同的值时,我还应该同时滚动浏览另一个spinbox中的不同值。

然而,实际发生的是滚动其中一个旋转框 滚动浏览另一个,但我只能看到单个字符,而不是整个单词。所以似乎索引对应于字符数而不是行。我怎样才能让它与线条对应?

1 个答案:

答案 0 :(得分:1)

问题是因为Thing1()Thing2()作为一个字符串返回everthing而spinbox需要列表 - 所以你必须将此字符串转换为列表

return output.strip().split()

我正在使用的代码(经过一些修改)

import tkinter as tk
import re

def thing(patternA, patternB):
    read_thing = open('file.txt', 'r')
    read_names = read_thing.read()

    print(read_names)

    multilineA = re.compile(patternA, re.MULTILINE)
    multilineB = re.compile(patternB, re.MULTILINE)

    for match in multilineA.finditer(read_names):
        s = match.start()
        e = match.end()
        print('[DEBUG] s,e:', s, e)

    for match in multilineB.finditer(read_names):
        s1 = match.start()
        e1 = match.end()
        print('[DEBUG] s1,e1:', s1, e1)
        output = read_names[e: s1]
        print('[DEBUG] output:', output)
        return output.strip().split("\n")

def thing1():
    pattern3 =r'^(............)$\
^(TITLE 1)$\
^(............)$'
    pattern4 = r'^(............)$\
^(TITLE 2)$\
^(............)$'

    return thing(pattern3, pattern4)

def thing2():
    pattern4 =r'^(............)$\
^(TITLE 2)$\
^(............)$'
    pattern5 = r'^(............)$\
^(TITLE 3)$\
^(............)$'

    return thing(pattern4, pattern5)

def sb1_changed():
    selected = var1.get()
    idx = values1.index(selected)
    print('selected:', selected)
    print('index', idx)
    var2.set(values2[idx])

def sb2_changed():
    selected = var2.get()
    idx = values2.index(selected)
    print('selected:', selected)
    print('index', idx)
    var1.set(values1[idx])

# --- main ---

values1 = thing1()
values2 = thing2()

print('[DEBUG] values1:', values1)
print('[DEBUG] values2:', values2)

root = tk.Tk()

var1 = tk.StringVar()
var2 = tk.StringVar()

spinbox1 = tk.Spinbox(root, textvariable=var1, values=values1,
                                   command=sb1_changed)
spinbox1.pack()

spinbox2 = tk.Spinbox(root, textvariable=var2, values=values2,
                                   command=sb2_changed)
spinbox2.pack()

root.mainloop()

file.txt进行测试

AA        01
AA        02
AA        03
AA        04
AA        05
TITLE 1
BB        01
BB        02
BB        03
BB        04
BB        05
TITLE 2
CC        01
CC        02
CC        03
CC        04
CC        05
TITLE 3
DD        01
DD        02
DD        03
DD        04
DD        05
TITLE 4
EE        01
EE        02
EE        03
EE        04
EE        05