0

我对编程很陌生,遇到一个问题,我必须打印出客户想要什么类型的面包,以及他们选择的调味品。我不知道如何在文本框中打印出选中/选中的项目。这就是我到目前为止所拥有的。

from Tkinter import *

class Application(Frame):
    def __init__ (self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widget()


    def create_widget(self):
        self.lbl = Label(self, text="Place your order").grid(row=0,column=2,columnspan=4)
        self.lbl2 = Label(self, text="Customer Name:").grid(row=1,column=0,columnspan=4,sticky=W)
        self.lbl3 = Label(self, text="Select your bun:").grid(row=2, column =0, columnspan=3, sticky=W)
        self.lbl4 = Label(self, text="Condiments:").grid(row=3, column =0, columnspan=3, sticky=W)

        self.bun = StringVar()
        self.bun.set(None)

        self.radio_bttn = Radiobutton(self, text = "White", variable = self.bun, value=1).grid(row=2, column = 1, columnspan=4)
        self.radio_bttn2 = Radiobutton(self, text = "Wheat", variable = self.bun, value=2).grid(row=2, column= 2, columnspan=4)

        self.work = Text(self,width=50,height=5).grid(row=12, column = 0, columnspan = 6)
        self.work1 = Text(self,width=40,height=1).grid(row=1,column=3, columnspan=4)

        self.cheese = BooleanVar()
        Checkbutton(self, text="Cheese", variable=self.cheese, onvalue="yes", offvalue="no").grid(row=4, column=0, sticky = W)
        self.lettuce = BooleanVar()
        Checkbutton(self, text="Lettuce", variable=self.lettuce, onvalue="yes", offvalue="no").grid(row=5, column=0, sticky = W)
        self.onion = BooleanVar()
        Checkbutton(self, text="Onion", variable=self.onion, onvalue="yes", offvalue="no").grid(row=6, column=0, sticky = W)
        self.tomato = BooleanVar()
        Checkbutton(self, text="Tomato", variable=self.tomato, onvalue="yes", offvalue="no").grid(row=7, column=0, sticky = W)
        self.pickles = BooleanVar()
        Checkbutton(self, text="Pickles", variable=self.pickles, onvalue="yes", offvalue="no").grid(row=8, column=0, sticky = W)
        self.mustard = BooleanVar()
        Checkbutton(self, text="Mustard", variable=self.mustard, onvalue="yes", offvalue="no").grid(row=9, column=0, sticky = W)
        self.mayo = BooleanVar()
        Checkbutton(self, text="Mayo", variable=self.mayo, onvalue="yes", offvalue="no").grid(row=10, column=0, sticky = W)
        self.ketchup = BooleanVar()
        Checkbutton(self, text="Ketchup", variable=self.ketchup, onvalue="yes", offvalue="no").grid(row=11, column=0, sticky = W)

        self.bttn_bttn = Button(self, text="Order", command = callback).grid(row =13, column = 0, sticky = W)


    def callback(self):
        toppings = "Here are your toppings.\n"

        if self.cheese.get():
            toppings += "Cheese\n"
        if self.lettuce.get():
            toppings += "Lettuce\n"
        if self.onion.get():
            toppings += "Onion\n"
        if self.tomato.get():
            toppings += "Tomato\n"
        if self.pickles.get():
            toppings += "Pickles\n"
        if self.mustard.get():
            toppings += "Mustard\n"
        if self.mayo.get():
            toppings += "Mayo\n"
        if self.ketchup.get():
            toppings += "Ketchup\n"

       self.work.insert(0.0, toppings)


#main

root= Tk()

root.title("Burger")

root.geometry("400x500")

app = Application(root)

root.mainloop(
4

1 回答 1

0

.grid任何 Tkinter 小部件的方法总是返回None.

考虑到这一点,您需要将这些调用.grid放在自己的行中:

self.work = Text(self,width=50,height=5)
self.work.grid(row=12, column = 0, columnspan = 6)
self.work1 = Text(self,width=40,height=1)
self.work1.grid(row=1,column=3, columnspan=4)

以下是您的脚本的工作副本。我所做的更改在评论框中:

from Tkinter import *

class Application(Frame):
    def __init__ (self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widget()


    def create_widget(self):
        self.lbl = Label(self, text="Place your order").grid(row=0,column=2,columnspan=4)
        self.lbl2 = Label(self, text="Customer Name:").grid(row=1,column=0,columnspan=4,sticky=W)
        self.lbl3 = Label(self, text="Select your bun:").grid(row=2, column =0, columnspan=3, sticky=W)
        self.lbl4 = Label(self, text="Condiments:").grid(row=3, column =0, columnspan=3, sticky=W)

        self.bun = StringVar()
        self.bun.set(None)

        self.radio_bttn = Radiobutton(self, text = "White", variable = self.bun, value=1).grid(row=2, column = 1, columnspan=4)
        self.radio_bttn2 = Radiobutton(self, text = "Wheat", variable = self.bun, value=2).grid(row=2, column= 2, columnspan=4)

        ##################################################
        self.work = Text(self,width=50,height=5)
        self.work.grid(row=12, column = 0, columnspan = 6)
        self.work1 = Text(self,width=40,height=1)
        self.work1.grid(row=1,column=3, columnspan=4)
        ##################################################

        self.cheese = BooleanVar()
        Checkbutton(self, text="Cheese", variable=self.cheese, onvalue="yes", offvalue="no").grid(row=4, column=0, sticky = W)
        self.lettuce = BooleanVar()
        Checkbutton(self, text="Lettuce", variable=self.lettuce, onvalue="yes", offvalue="no").grid(row=5, column=0, sticky = W)
        self.onion = BooleanVar()
        Checkbutton(self, text="Onion", variable=self.onion, onvalue="yes", offvalue="no").grid(row=6, column=0, sticky = W)
        self.tomato = BooleanVar()
        Checkbutton(self, text="Tomato", variable=self.tomato, onvalue="yes", offvalue="no").grid(row=7, column=0, sticky = W)
        self.pickles = BooleanVar()
        Checkbutton(self, text="Pickles", variable=self.pickles, onvalue="yes", offvalue="no").grid(row=8, column=0, sticky = W)
        self.mustard = BooleanVar()
        Checkbutton(self, text="Mustard", variable=self.mustard, onvalue="yes", offvalue="no").grid(row=9, column=0, sticky = W)
        self.mayo = BooleanVar()
        Checkbutton(self, text="Mayo", variable=self.mayo, onvalue="yes", offvalue="no").grid(row=10, column=0, sticky = W)
        self.ketchup = BooleanVar()
        Checkbutton(self, text="Ketchup", variable=self.ketchup, onvalue="yes", offvalue="no").grid(row=11, column=0, sticky = W)

        #####################################################
        # Just a quick fix here:
        # You need to set 'command' to 'self.callback', not 'callback'
        self.bttn_bttn = Button(self, text="Order", command = self.callback).grid(row =13, column = 0, sticky = W)
        #####################################################


    def callback(self):
        toppings = "Here are your toppings.\n"

        if self.cheese.get():
            toppings += "Cheese\n"
        if self.lettuce.get():
            toppings += "Lettuce\n"
        if self.onion.get():
            toppings += "Onion\n"
        if self.tomato.get():
            toppings += "Tomato\n"
        if self.pickles.get():
            toppings += "Pickles\n"
        if self.mustard.get():
            toppings += "Mustard\n"
        if self.mayo.get():
            toppings += "Mayo\n"
        if self.ketchup.get():
            toppings += "Ketchup\n"

        self.work.insert(0.0, toppings)


#main

root= Tk()

root.title("Burger")

root.geometry("400x500")

app = Application(root)

root.mainloop()
于 2013-11-10T23:47:59.617 回答