Python制作GUI窗口

2024-09-25 08:35:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个程序,可以让用户选择3件事:

def createLeague(): #creates a list with all teams
   file = open('league1.txt', 'r')
   teams = []
   for line in file:
      team = line.split()
      teams.append(team)
   file.close()
   return teams

def getTeam(teams): #this function gets a team that user inputs
   result = ' '
   choice = input('Enter the team: ')
   checkforteam = False 
   for line in teams:
        team = line[0] 
        if choice == team: #check for input team in all lines
           result = team               
           games = line[1]
           wins = line[2]     #assign all statistics to the variables with
           draws = line[3]    #appropriate names
           loses = line[4]
           checkforteam = True
   if checkforteam: #if it is True, it will return the team. If False, returns an error message
      print(result, games, wins, draws, loses)
   else:
      print('No such a team')

def getWinner(teams): #returns a leader
     winner = ' '
     result = 0
     loses = 100
     for team in teams:
         points = int(team[2])*3 + int(team[3])
         lose = int(team[4])#counting all points
         if points > result: #find a team with maximum points
            result = points
            winner = team[0]
            loses = lose 
         elif points == result:
            if lose < loses:
                winner = team[0]

print('Winner: ', winner)
print('Points: ', result)

def updateScore(teams): #update the table
    firsteam = input('Home: ')
    secondteam = input('Away: ')
    goal1 = int(input('Goals scored by home: '))
    goal2 = int(input('Goals scored by away: '))
    f = open('nhl.txt', 'w')
    for team in teams:
        komanda = team[0]
        matches = int(team[1])
        wins = int(team[2])
        draws = int(team[3])
        loses = int(team[4])
        if firsteam == komanda:
            if goal1 > goal2:
               matches += 1
               wins += 1
            elif goal1 == goal2:
               matches += 1
               draws += 1
            else:
               matches += 1
               loses += 1
        elif secondteam == komanda:
            if goal1 < goal2:
               matches += 1
               wins += 1
            elif goal1 == goal2:
               matches += 1
               draws += 1
            else:
               matches += 1
               loses += 1

        newline = komanda+ ' '+ str(matches) + ' ' +str(wins)+ ' '+    str(draws) + ' '+ str(loses)+ '\n'
        f.write(newline)
 f.close()
 print('Saved')


 teams = createLeague()

 loop = 1 #variable that makes 'while' working until user wants to close the program
 while loop == 1:
    print('0. Exit')
    print('1. Find a team')
    print('2. Get a leader')
    print('3. Update the results')
    x = input('Choose: ')
    if x == '1':
        getTeam(teams)
    elif x == '2':
        getWinner(teams)
    elif x == '3':
        updateScore(teams)
    elif x == '0':
        print('Goodbye!')
        loop = 0
    else:
        print('Wrong!')

现在我想,当我在IDLE中选择1、2或3时,会出现一个GUI窗口,在那里调用while循环的函数将工作。我被它绊倒了。我该怎么做?请给出一个函数的例子


Tags: theinputiflineresultteampointsint