简体   繁体   English

如何在 Tkinter GUI 中对包含不同小部件的两个不同框架应用相同的方法

[英]How to apply same method to two different frames, containing different widgets, in Tkinter GUI

I am making a cricket game where there is an option to select two teams for the match.我正在制作一场板球比赛,其中有两个球队参加比赛的 select 选项。 The user can select two teams from all available teams by clicking the next and previous buttons that change the image (actually the team's flag) and the team's name on every click.用户可以通过单击更改图像(实际上是团队的标志)和团队名称的下一个和上一个按钮,从所有可用团队中选择两个团队。 I have created a common function for the selection process.我为选择过程创建了一个通用的 function。 In that function/method, the labels and buttons are already present and packed.在该函数/方法中,标签和按钮已经存在并打包。 I only have to call it for team 1 and team 2 separately, by passing the allotted frames as an argument in the function.通过将分配的帧作为 function 中的参数传递,我只需为团队 1 和团队 2 分别调用它。

Now the problem is, after calling that function for the first side (Team1), when I call it for the second side (Team 2), the images and names are only changed for the second side.现在的问题是,在为第一方(Team1)调用 function 之后,当我为第二方(Team 2)调用它时,图像和名称仅在第二方更改。 The problem can be better understood by looking at the program because maybe I am unable to explain my issue.通过查看程序可以更好地理解这个问题,因为也许我无法解释我的问题。 I want both pairs of next and previous buttons to work independently for the selection of teams.我希望两对下一个和上一个按钮独立工作以选择团队。 I have tried to make a separate class for this thing, but that's totally an irrelevant solution and of course is not working.我试图为这个东西制作一个单独的 class ,但这完全是一个不相关的解决方案,当然是行不通的。

The piece of my code, extracted from my complete game code, is attached below.我的代码片段是从我的完整游戏代码中提取的,附在下面。

from tkinter import *

class CricketMatch(Tk):
    def __init__(self):
        super(CricketMatch, self).__init__()

        self.title("Cricket 2020")
        self.geometry('980x660+200-60')
        self.state('zoomed')

        selection_Frame = Frame(self)
        selection_Frame.pack()

        ### TEAM 1 CONTAINER ###
        Team1_Container = Frame(selection_Frame)
        Team1_Container.pack(side = LEFT)

        ButtonFrame = Frame(Team1_Container)
        self.select_Teams(Team1_Container, ButtonFrame)   #Function called for team1
        ButtonFrame.pack(pady = (20,0))

        vsLabel = Label(selection_Frame, text = 'VS')
        vsLabel.pack(side = LEFT, padx = 50)

        # ### TEAM 2 CONTAINER ###
        Team2_Container = Frame(selection_Frame)
        Team2_Container.pack()

        ButtonFrame2 = Frame(Team2_Container)
        self.select_Teams(Team2_Container, ButtonFrame2)   #Function called for team2
        ButtonFrame2.pack(pady = (20,0))

    def select_Teams(self, ImageFrame, ButtonFrame):
        self.teams = ['Afghanistan', 'Australia', 'Bangladesh', 'England', 'India',
                    'Ireland', 'Kenya', 'Netherlands', 'Newzealand', 'Pakistan',
                    'Scotland', 'SouthAfrica', 'Srilanka', 'UAE', 'WestIndies',
                    'Zimbabwe']
        self.cur_image = 0

        self.image = Label(ImageFrame, text = f"{self.teams[self.cur_image]}.png" , font=('', 12, 'bold'), bg='light blue')
        self.image.pack(ipady = 30)

        self.left = Button(ButtonFrame, text="<", fg='red', font=('', 11, 'bold'), command=self.lefttoggle)
        self.left.pack(side=LEFT)

        self.image_name=Label(ButtonFrame, text=self.teams[self.cur_image], font=('', 10,'bold'))
        self.image_name.pack(side=LEFT)

        self.right = Button(ButtonFrame, text=">", fg='red', font=('', 11, 'bold'), command=self.righttoggle)
        self.right.pack(side=LEFT)

    def lefttoggle(self):
        self.cur_image = (self.cur_image - 1) % len(self.teams)
        self.update_image()

    def righttoggle(self):
        self.cur_image = (self.cur_image + 1) % len(self.teams)
        self.update_image()

    def update_image(self):
        self.image.config(text=f"{self.teams[self.cur_image]}.png")
        self.image_name.config(text=self.teams[self.cur_image])


Match = CricketMatch()
Match.mainloop()

Since you used same set of instance variables for the two team selections, they will be effective to the second team selection.由于您为两个团队选择使用了相同的实例变量集,因此它们将对第二个团队选择有效。

You should encapsulate the team selection in a class:您应该将团队选择封装在 class 中:

class TeamSelector(Frame):
    teams = ['Afghanistan', 'Australia', 'Bangladesh', 'England', 'India',
             'Ireland', 'Kenya', 'Netherlands', 'Newzealand', 'Pakistan',
             'Scotland', 'SouthAfrica', 'Srilanka', 'UAE', 'WestIndies',
             'Zimbabwe']

    def __init__(self, master, *args, **kw):
        super().__init__(master, *args, **kw)
        self.cur_image = 0

        self.image = Label(self, text=f"{self.teams[self.cur_image]}.png", font=('', 12, 'bold'), bg='light blue')
        self.image.pack(ipady=30, fill=X)

        btn_frame = Frame(self)
        btn_frame.pack(pady=(20,0))

        self.left = Button(btn_frame, text="<", fg='red', font=('', 11, 'bold'), command=self.left_toggle)
        self.left.pack(side=LEFT)

        self.image_name = Label(btn_frame, text=self.teams[self.cur_image], font=('', 10,'bold'), width=20)
        self.image_name.pack(side=LEFT)

        self.right = Button(btn_frame, text=">", fg='red', font=('', 11, 'bold'), command=self.right_toggle)
        self.right.pack(side=LEFT)

    def left_toggle(self):
        self.cur_image = (self.cur_image - 1) % len(self.teams)
        self.update_image()

    def right_toggle(self):
        self.cur_image = (self.cur_image + 1) % len(self.teams)
        self.update_image()

    def update_image(self):
        self.image.config(text=f"{self.teams[self.cur_image]}.png")
        self.image_name.config(text=self.teams[self.cur_image])

Then use this class to create instances for team 1 and team 2 selections:然后使用此 class 为团队 1 和团队 2 选择创建实例:

class CricketMatch(Tk):
    def __init__(self):
        super().__init__()

        self.title("Cricket 2020")
        self.geometry('980x660+200-60')
        self.state('zoomed')

        selection_Frame = Frame(self)
        selection_Frame.pack()

        ### TEAM 1 SELECTOR ###
        self.team1 = TeamSelector(selection_Frame)
        self.team1.pack(side=LEFT)

        vsLabel = Label(selection_Frame, text = 'VS')
        vsLabel.pack(side=LEFT, padx=50)

        ### TEAM 2 SELECTOR ###
        self.team2 = TeamSelector(selection_Frame)
        self.team2.pack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM