如何从单选按钮和复选框中获取文本以显示在我的故事中?

时间:2017-03-05 23:45:40

标签: python tkinter

另外,如何使用网格将所有按钮和框放在同一行上? 每当我尝试使用此代码获取文本时,它会显示为0或1,每当我按下故事时,部分内容都不会显示在{}内部。我该如何解决这个问题?

import Tkinter

class StoryMaker():
    def __init__(self):
        self.main_window = Tkinter.Tk()

        self.top_frame = Tkinter.Frame (self.main_window)
        self.bottom_frame = Tkinter.Frame (self.main_window)

        message = "Please enter information for a new story, then click the 'Make Story' button."
        self.message_label = Tkinter.Label (self.top_frame, text = message)

        self.message_label.pack (anchor = 'w')

        self.name_label = Tkinter.Label (self.top_frame, text = 'Name:')
        self.name_entry = Tkinter.Entry (self.top_frame, width = 20)

        self.name_label.pack(anchor = 'w')
        self.name_entry.pack(anchor = 'w')

        self.friend_label = Tkinter.Label (self.top_frame, text = "Friend's name:")
        self.friend_entry = Tkinter.Entry (self.top_frame, width = 20)

        self.friend_label.pack(anchor = 'w')
        self.friend_entry.pack(anchor = 'w')

        self.sport_label = Tkinter.Label (self.top_frame, text = "Sport:")
        self.sport_entry = Tkinter.Entry (self.top_frame, width = 20)

        self.sport_label.pack(anchor = 'w')
        self.sport_entry.pack(anchor = 'w')

        self.adjective_label = Tkinter.Label (self.bottom_frame, text = "Adjective(s): ")

        self.adjective_label.pack(anchor = 'w')

        self.cb_var1 = Tkinter.IntVar()
        self.cb_var2 = Tkinter.IntVar()
        self.cb_var3 = Tkinter.IntVar()

        self.cb_var1.set(0)
        self.cb_var2.set(0)
        self.cb_var3.set(0)

        self.cb1 = Tkinter.Checkbutton(self.bottom_frame, text = "fun" , variable = self.cb_var1)
        self.cb2 = Tkinter.Checkbutton(self.bottom_frame, text = "exciting" , variable = self.cb_var2)
        self.cb3 = Tkinter.Checkbutton(self.bottom_frame, text = "new" , variable = self.cb_var3)

        self.cb1.pack()
        self.cb2.pack()
        self.cb3.pack()

        self.body_label = Tkinter.Label (self.bottom_frame, text = "Body Part: ")

        self.body_label.pack(anchor = 'w')

        self.radio_var = Tkinter.IntVar()
        self.radio_var.set(1)

        self.rb1 = Tkinter.Radiobutton(self.bottom_frame, text = "hands" , variable = self.radio_var, value = 1, command = self.make_story)
        self.rb2 = Tkinter.Radiobutton(self.bottom_frame, text = "knees" , variable = self.radio_var, value = 2, command = self.make_story)
        self.rb3 = Tkinter.Radiobutton(self.bottom_frame, text = "bottom" , variable = self.radio_var, value = 3, command = self.make_story)

        self.rb1.pack()
        self.rb2.pack()
        self.rb3.pack()

        self.make_button = Tkinter.Button(self.bottom_frame, text = "Make Story!", command = self.make_story)

        self.make_button.pack(anchor = 'w')

        self.top_frame.pack(anchor = 'w')
        self.bottom_frame.pack(anchor = 'w')

        Tkinter.mainloop()

    def make_story(self):
        story = "My name is",self.name_entry.get()," and my friend's name is",self.friend_entry.get(),". We are playing a",self.cb_var1.get(),self.cb_var2.get(),self.cb_var3.get(),"sport called",self.sport_entry.get(),"until",self.friend_entry.get(),"fell on their",self.radio_var.get(),". We ended up going home soon afterwards but at least we had fun."

        self.story_label = Tkinter.Label (self.bottom_frame, text = story)

        self.story_label.pack(anchor = "w")

story_maker = StoryMaker()

1 个答案:

答案 0 :(得分:1)

您可以使用 <div class="box-body"> <h2>Search Database</h2> <input class="form-control" type="text" name="search" id="search" placeholder="search our inventory"> <br> <br> <h2 class="bg-success" id="result"> </h2> <script type="text/javascript"> $('#search').keyup(function(){ var search = $('#search').val(); $.ajax({ url:'searchproditem.php', data:{search:search}, type: 'POST', success:function(data){ if(!data.error) { $('#result').html(data); $('#result li').click(function(){ var res_value = $(this).text(); $('#search').attr('value', res_value); }); } } }); }); </script> <?php include 'db/db.php'; $search = $_POST['search']; if (!empty($search)) { $res = $con->prep("SELECT * FROM items WHERE itemname LIKE :search "); $res->bindValue(':search', "$search%"); $res->execute(); $count = $res->rowCount(); if (!$res) { die('QUERY FAILED'); } if ($count <= 0) { echo "Sorry We dont have that item in stock"; }else{ while ($r = $res->fetch(PDO::FETCH_ASSOC)) { $brand = $r['itemname']; ?> <ul class="list-unstyled"> <?php echo "<li>{$brand} in stock</li>"; ?> </ul> <?php } } } ?> 访问大多数小部件的相应属性;在这种情况下,<insert widget here>.cget("attribute")。它适用于复选框和单选按钮,至少。