Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 特金特环函数_Python_Loops_User Interface_Tkinter - Fatal编程技术网

Python 特金特环函数

Python 特金特环函数,python,loops,user-interface,tkinter,Python,Loops,User Interface,Tkinter,我是GUI编程新手,我正在尝试让这个python程序正常工作。调用函数start()以更新两个画布的正确方法是什么。我的代码看起来像这样 from Tkinter import * class TKinter_test(Frame): def __init__(self,parent): Frame.__init__(self,parent) self.parent = parent self.initUI() user_reply = 0

我是GUI编程新手,我正在尝试让这个python程序正常工作。调用函数
start()
以更新两个画布的正确方法是什么。我的代码看起来像这样

from Tkinter import *
class TKinter_test(Frame):
   def __init__(self,parent):
       Frame.__init__(self,parent)
       self.parent = parent
       self.initUI()

   user_reply = 0


   def accept(self):
       self.user_reply = 1
   def decline(self):
       self.user_reply = 2

   def initUI(self):
       BtnFrame = Frame (self.parent)
       BtnFrame.place(y=450, x=20)

       canvasFrame = Frame(self.parent)
       canvasFrame.place(y = 200)
       canvas_1 = Canvas(canvasFrame, width = "220", height ="200")
       canvas_2 = Canvas(canvasFrame, width = "220", height ="200")
       canvas_1.pack(side = LEFT)
       canvas_2.pack(side = RIGHT)  

       textfield_1 = canvas_1.create_text(30,50,anchor = 'w', font =("Helvetica", 17))
       textfield_2 = canvas_2.create_text(30,50,anchor = 'w', font =("Helvetica", 17))
       Accept = Button(BtnFrame, text="Friends!", width=25, command = self.accept)
       Decline = Button(BtnFrame, text="Not friends...", width=25, command = self.decline)
       Accept.pack(side = LEFT)
       Decline.pack(side = RIGHT)

   def ask_user(self,friend_1,friend_2):
       timer = 0;

       while(timer == 0):
           if(self.user_reply == 1):
               print friend_1 + " and " + friend_2 + " are friends!"
               self.user_reply = 0
               timer = 1;
           elif(user_reply == 2):
               print friend_1 + " and " + friend_2 + " are not friends..."
               self.user_reply = 0
               timer = 1

   def start(self):
       listOfPeople = ["John","Tiffany","Leo","Tomas","Stacy","Robin","Carl"]
       listOfPeople_2 = ["George","Jasmin","Rosa","Connor","Valerie","James","Bob"]
       for friend_1 in listOfPeople:
           for friend_2 in listOfPeople_2:
               ask_user(friend_1,friend_2)
       print "Finished"

def main():
    root = Tk()
    root.geometry("500x550")
    root.wm_title("Tkinter test")
    app = TKinter_test(root)
    root.mainloop()

if __name__ == '__main__':
main()
我想在ask_用户中使用一些更新textfield_1和2的内容。类似于
textfield\u 1.itemconfigure(text=friend\u 1)
的东西,我不希望使用线程。
谢谢。

做事件驱动编程需要不同的心态,一开始可能会有点沮丧和困惑。我建议您查看一下关于堆栈溢出的高分Tkinter答案中的代码,并对其进行实验,进行一些小的更改,看看会发生什么。随着你对它越来越熟悉,它将开始变得有意义

我不知道您为什么需要所有这些
Frame
Canvas
对象,因此我简化了GUI,使用单个
Frame
,“借用”Bryan Oakley的模板。我没有使用
Canvas
文本项来显示朋友的名字,而是使用简单的
Label
小部件

我没有将好友列表硬编码到GUI类中,而是将它们以元组形式作为关键字参数传递给GUI构造函数,
friendlists
。在将
kwargs
传递到
帧之前,我们必须从
kwargs
中删除该参数。由于该方法将关键字args视为错误,因此无法识别

我创建了一个生成器表达式
self.pairs
,它使用double
for
循环来生成成对的朋友名。调用
next(self.pairs)
可以得到下一对朋友的名字

当按下
按钮
时,根据按下“friends!”还是“Not friends.”按钮,将调用
test_friends
方法,并使用
reply
参数
True
False

test\u friends
打印当前一对好友的信息,然后调用
set\u friends
方法在
标签中设置下一对好友的姓名。如果没有剩余的对,则程序退出

import Tkinter as tk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        # Extract the friend lists from the keyword args 
        friends1, friends2 = kwargs['friendlists']
        del kwargs['friendlists']

        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        # A generator that yields all pairs of people from the friends lists
        self.pairs = ((u, v) for u in friends1 for v in friends2)

        # A pair of labels to display the names
        self.friend1 = tk.StringVar()
        tk.Label(self, textvariable=self.friend1).grid(row=0, column=0)

        self.friend2 = tk.StringVar()
        tk.Label(self, textvariable=self.friend2).grid(row=0, column=1)

        # Set the first pair of names
        self.set_friends()

        cmd = lambda: self.test_friends(True)
        b = tk.Button(self, text="Friends!", width=25, command=cmd)
        b.grid(row=1, column=0)

        cmd = lambda: self.test_friends(False)
        b = tk.Button(self, text="Not friends.", width=25, command=cmd)
        b.grid(row=1, column=1)

    def set_friends(self):
        # Set the next pair of names
        f1, f2 = next(self.pairs)
        self.friend1.set(f1)
        self.friend2.set(f2)

    def test_friends(self, reply):
        f1, f2 =  self.friend1.get(), self.friend2.get()
        reply = (' not ', ' ')[reply]
        print '%s and %s are%sfriends' % (f1, f2, reply)
        try:
            self.set_friends()
        except StopIteration:
            # No more pairs
            print 'Finished!'
            self.parent.destroy()


def main():
    people_1 = [
        "John", 
        "Tiffany", 
        "Leo", 
        "Tomas", 
        #"Stacy", 
        #"Robin", 
        #"Carl",
    ]

    people_2 = [
        "George", 
        "Jasmin", 
        "Rosa", 
        #"Connor", 
        #"Valerie", 
        #"James", 
        #"Bob",
    ]

    root = tk.Tk()
    root.wm_title("Friend Info")
    app = MainApplication(root, friendlists=(people_1, people_2))
    app.pack()
    root.mainloop()

if __name__ == "__main__":
    main()

做事件驱动编程需要不同的思维方式,一开始可能有点令人沮丧和困惑。我建议您查看一下关于堆栈溢出的高分Tkinter答案中的代码,并对其进行实验,进行一些小的更改,看看会发生什么。随着你对它越来越熟悉,它将开始变得有意义

我不知道您为什么需要所有这些
Frame
Canvas
对象,因此我简化了GUI,使用单个
Frame
,“借用”Bryan Oakley的模板。我没有使用
Canvas
文本项来显示朋友的名字,而是使用简单的
Label
小部件

我没有将好友列表硬编码到GUI类中,而是将它们以元组形式作为关键字参数传递给GUI构造函数,
friendlists
。在将
kwargs
传递到
帧之前,我们必须从
kwargs
中删除该参数。由于该方法将关键字args视为错误,因此无法识别

我创建了一个生成器表达式
self.pairs
,它使用double
for
循环来生成成对的朋友名。调用
next(self.pairs)
可以得到下一对朋友的名字

当按下
按钮
时,根据按下“friends!”还是“Not friends.”按钮,将调用
test_friends
方法,并使用
reply
参数
True
False

test\u friends
打印当前一对好友的信息,然后调用
set\u friends
方法在
标签中设置下一对好友的姓名。如果没有剩余的对,则程序退出

import Tkinter as tk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        # Extract the friend lists from the keyword args 
        friends1, friends2 = kwargs['friendlists']
        del kwargs['friendlists']

        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        # A generator that yields all pairs of people from the friends lists
        self.pairs = ((u, v) for u in friends1 for v in friends2)

        # A pair of labels to display the names
        self.friend1 = tk.StringVar()
        tk.Label(self, textvariable=self.friend1).grid(row=0, column=0)

        self.friend2 = tk.StringVar()
        tk.Label(self, textvariable=self.friend2).grid(row=0, column=1)

        # Set the first pair of names
        self.set_friends()

        cmd = lambda: self.test_friends(True)
        b = tk.Button(self, text="Friends!", width=25, command=cmd)
        b.grid(row=1, column=0)

        cmd = lambda: self.test_friends(False)
        b = tk.Button(self, text="Not friends.", width=25, command=cmd)
        b.grid(row=1, column=1)

    def set_friends(self):
        # Set the next pair of names
        f1, f2 = next(self.pairs)
        self.friend1.set(f1)
        self.friend2.set(f2)

    def test_friends(self, reply):
        f1, f2 =  self.friend1.get(), self.friend2.get()
        reply = (' not ', ' ')[reply]
        print '%s and %s are%sfriends' % (f1, f2, reply)
        try:
            self.set_friends()
        except StopIteration:
            # No more pairs
            print 'Finished!'
            self.parent.destroy()


def main():
    people_1 = [
        "John", 
        "Tiffany", 
        "Leo", 
        "Tomas", 
        #"Stacy", 
        #"Robin", 
        #"Carl",
    ]

    people_2 = [
        "George", 
        "Jasmin", 
        "Rosa", 
        #"Connor", 
        #"Valerie", 
        #"James", 
        #"Bob",
    ]

    root = tk.Tk()
    root.wm_title("Friend Info")
    app = MainApplication(root, friendlists=(people_1, people_2))
    app.pack()
    root.mainloop()

if __name__ == "__main__":
    main()

不清楚您希望如何执行更新。好吧,每次函数
start()
调用函数
ask\u user()时
我想让它用好友列表的两个名字更新视图,并等待用户通过按钮Accept/Decept@BillalBEGUERADJSorry回答他们是否是好友,因为响应太晚,但希望我的代码会有所帮助。不清楚您希望如何执行更新。好吧,每次函数
start()
调用函数
ask_user()
时,我希望它使用好友列表的两个名称更新视图,并等待用户回答他们是否是好友,按钮接受/拒绝@BillalBEGUERADJSorry,以防响应延迟,但希望我的代码能有所帮助。谢谢!我相信我可以用这个来完成我的程序。谢谢!我肯定我能用这个来完成我的程序。