简体   繁体   English

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

[英]How do i get text from a radio button and a check box to show up in my story?

Also, how do I put all the buttons and boxes on the same line w/o using a grid? 另外,如何不使用网格将所有按钮和框放在同一行上,而没有网格? Whenever i try to get text using this code it shows up as 0 or 1 and whenever i press make story the parts im not getting show up inside {}. 每当我尝试使用此代码获取文本时,它都会显示为0或1,并且每当我按下make story时,部分即时消息都不会显示在{}中。 How do i fix that too? 我也该如何解决?

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()

You can use <insert widget here>.cget("attribute") to access the corresponding property of most widgets; 您可以使用<insert widget here>.cget("attribute")来访问大多数窗口小部件的相应属性。 in this case, "text" . 在这种情况下为"text" It works for checkboxes and radio buttons, at least. 至少适用于复选框和单选按钮。

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

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