用Tkinter连接python脚本
我想为NameSort脚本设计一个Gui脚本 我想用Gui开发一个python3脚本。这是我的Cli代码用Tkinter连接python脚本,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我想为NameSort脚本设计一个Gui脚本 我想用Gui开发一个python3脚本。这是我的Cli代码 text = open('/Users/test/Desktop/readme.txt','r') def readtxt(txt): #turns txt to ls dictls=(txt.read()).splitlines() return dictls def getdict(dictls): #turns to dictionary dict
text = open('/Users/test/Desktop/readme.txt','r')
def readtxt(txt): #turns txt to ls
dictls=(txt.read()).splitlines()
return dictls
def getdict(dictls): #turns to dictionary
dict1 = {dictls.index(i) : i for i in dictls}
return dict1
def getkey(diction,index): #getkey
return diction[index]
def randomord(x,z): #random order generator, won't generate repeditive numbers
import random
output = []
done = []
y = 0 #already generated
while y <= x:
rnum = random.randint(0,z)
if rnum not in done:
output.append(rnum)
done.append(rnum)
y+=1
return output
def main():
ls=readtxt(text)
while True:
print(f'\nThere are {len(ls)} names on the list.')
try:
h = int(input('Number of names to gen: '))
if h-1 <= len(ls)-1:
outls = [getkey(getdict(ls),i) for i in randomord(h-1,len(ls)-1)]
print('\n'.join(outls))
else:
print(f'[*]ERR: There are only {len(ls)} names on the list.')
except:
print('[*]ERR')
main()
text=open('/Users/test/Desktop/readme.txt',r')
def readtxt(txt):#将txt转换为ls
dictls=(txt.read()).splitlines()
返回命令
def getdict(dictls):#转到字典
dict1={dictls.index(i):dictls中的i代表i}
返回指令1
def getkey(措辞、索引):#getkey
返回用语[索引]
def randomord(x,z):#随机顺序生成器,不会生成重复数
随机输入
输出=[]
完成=[]
y=0#已生成
当y在tkinter中设置按钮的功能时,您仅在命令中传递功能本身,即
command = test1,
不是command=test1()
,因为在本例中,您只是给它函数的结果(即None
)。这样,它可以在每次按下按钮时执行该功能
如果要在参数中为其执行提供参数,则必须使用lambda函数:
command = lambda: test1(param1, param2),
如果您感兴趣,可以在tkinter的按钮上找到合适的指南
command = lambda: test1(param1, param2),