zuozuoxiong
zuozuoxiong

Reputation: 71

Connect a python script with Tkinter

I want to design a Gui script for a NameSort script.

I want to develop a python3 script with Gui. Here is my code for Cli only

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()

Now I have tried these code

text = open('/root/Desktop/Python/gui/hell.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')
global startmain
startmain = 0

def test1():
   startmain = 1




###########################
##########################
###################################
from tkinter import *
import tkinter as tk





window = tk.Tk()


p1 = tk.Label(
   text="Thomas-name-sort",
   fg="red",
   bg='green',
   width=10,
   height=8
)

name_ent = tk.Label(text="输入生成数量Number of names to gen:")
entry = tk.Entry()
name_ent.pack()
entry.pack()

name = entry.get()


b1 = tk.Button(
   text="auto-testing自检",
   bg="blue",
   fg="orange",
   width=20,
   height=5,
   command=test1()
   
) 
b1.pack()

if startmain == 1:
   main()
#bind-zone

window.mainloop()

It does not work. I want to design a Gui script for a NameSort script. I could not bind the button with the function T have tried "command=main()" and "button.bind("", func=main()"

Please help me!!!

Upvotes: 1

Views: 195

Answers (1)

Rolv Apneseth
Rolv Apneseth

Reputation: 2118

To set a button's function in tkinter, you only pass the function itself in the command parameter i.e.

command = test1,

Not command = test1() as in this case you are just giving it the result of the function (which is None). This is so that it can execute the function each time the button is pressed.

If you want to provide in arguments for it to execute, you'll then have to use a lambda function:

command = lambda: test1(param1, param2),

You can find decent guides on buttons in tkinter here if you're interested

Upvotes: 1

Related Questions