현재 Tkinter/Python GUI에서 사용자 입력을 가져 와서 순열을 인쇄하는 프로젝트를 진행 중입니다.Python/Tkinter Permutation 빈 필드를 건너 뛰는 방법
사용자는 입력의 순열을 뱉어내는 6 개의 입력 필드를 채울 수 있습니다. 이 기능은 완벽하게 작동합니다.
그러나 사용자가 몇 개의 입력란을 비워두면 공백으로 인해 중복되는 문제가 발생합니다.
비어있는 입력 필드를 건너 뛰고 무시하도록하려면 어떻게해야합니까? 출력은 3 개의 필드가 공백으로 남을 경우, 예를 들어, 6 대신에 3 워드의 순열과 같은, 엔트리를 갖는 필드의 순열 일 것이다.
from itertools import permutations
from itertools import chain
from tkinter import *
import re
fields = 'Campaign', 'Ad _Group', 'Location', 'Aux_Groups', 'Aux_Groups2',
'Aux_Groups3'
#makeForm Function
def makeForm(root, fields):
entries = []
for field in fields:
row = Frame(root)
lab = Label(row, width=20, text=field, anchor='w')
ent = Entry(row)
row.pack(side=TOP, fill=X, padx=10, pady=10)
lab.pack(side=LEFT)
ent.pack(side=RIGHT, expand=YES, fill=X)
entries.append((field, ent))
return entries
#ExactMatch Function.
def exactMatch(entries):
words = [entry[1].get() for entry in entries]
perms = [p for p in permutations((words))]
x1 = str(perms)
perms2 = x1.replace("," , '') #Takes out the Quotations
perms3 = perms2.replace("'" , '') #Takes out the Quotations
perms4 = perms3.replace("(" , '[')
perms5 = perms4.replace(')' , ']\n')
perms6 = perms5.replace (" ", "")
print("Exact Match:")
print('------------')
print(perms6)
if __name__ == '__main__':
root = Tk()
ents = makeForm(root, fields)
root.bind('<Return>', (lambda event, e=ents: fetch(e)))
#Exact Match Button
b2 = Button(root, text='Exact Match', command=(lambda e=ents: exactMatch(e)))
b2.pack(side=LEFT, padx=10, pady=10)
#Quit Button
b6 = Button(root, text='Quit', command=root.quit)
b6.pack(side=LEFT, padx=10, pady=10)
root.mainloop()
만 *하지 빈 값을 허용하도록 이해 목록을 고칠 수 *를, 하지만 어쩌면 입력 단어 (공백, 기호 ...)를 정리하고 싶습니까? (또한 콘솔에 무엇인가를 인쇄하는 대신 tkinter에서 레이블을 업데이트해야합니다.) – PRMoureu
빈 공간을 제거하기 위해 항목에서 값을 얻으면'.replace ("", "")를 추가 할 수 있습니다. ('words = [entry [1] .get(). ("", "") entry에있는 항목을 대체하십시오)') – SneakyTurtle
@PRMoureu 조언 해 주셔서 감사합니다! 내 코드, 레이블, 버튼 등을 많이 버려 두었습니다. –