Kivy绘图应用报错:method与str无法拼接及计时器启动需求
Hey there! Let's break down what's going on with your error and fix it step by step, since you're just getting started with Python and Kivy.
First: Understand the TypeError
That TypeError: unsupported operand type(s) for +: 'method' and 'str' error happens because you're trying to concatenate a method object (like a function defined in your MainScreen class) with a string. When you tried wrapping it in str(), you got that weird bound method... text because you're converting the method itself to a string, not the value you actually want (the username from your TextInput).
Example of What You Probably Did (Wrong):
# Bad: Using the method/control object instead of its text property filename = self.get_username + ".txt" # get_username is a method, not its return value # OR filename = self.username_input + ".txt" # username_input is the TextInput widget, not its text
The Fix for Filename Saving
You need to grab the text property of your TextInput widget, which holds the actual username the user typed. If you defined the TextInput in your kv file with an id, use self.ids to access it:
Step 1: Update Your Save Logic
class MainScreen(Screen): def save_drawing(self): # Get the username from the TextInput (strip whitespace to avoid empty names) username = self.ids.username_input.text.strip() # Add a check to make sure the user actually entered a name if not username: # Optional: Add a label to show an error message self.ids.status_label.text = "Please enter a username first!" return # Create the filename using the username filename = username + ".txt" # Write your drawing data to the file (replace drawing_content with your actual data) drawing_content = "Your drawing data here (e.g., coordinates, shapes)" with open(filename, 'w') as f: f.write(drawing_content) self.ids.status_label.text = f"Drawing saved as {filename}!"
Step 2: Update Your KV Layout
Make sure your TextInput has an id so you can reference it in Python:
<MainScreen>: BoxLayout: orientation: 'vertical' # Username input with an ID TextInput: id: username_input hint_text: "Enter your username" # Submit button to start timer Button: text: "Submit" on_press: root.start_timer() # Save button to trigger save logic Button: text: "Save Drawing" on_press: root.save_drawing() # Optional: Status label to show messages Label: id: status_label text: "Ready to start!"
Second: Start Timer Only When Submit is Clicked
To trigger the timer after clicking Submit, bind the button's on_press event to a method that starts the timer using Kivy's Clock module:
Add Timer Logic to MainScreen
from kivy.clock import Clock class MainScreen(Screen): def __init__(self, **kwargs): super().__init__(**kwargs) self.timer_event = None # Store the timer event to control it later self.timer_count = 0 # Track timer time def start_timer(self): # Optional: Check for username here too if you want username = self.ids.username_input.text.strip() if not username: self.ids.status_label.text = "Enter a username to start the timer!" return # Cancel existing timer if it's running (avoid multiple timers) if self.timer_event: self.timer_event.cancel() # Start the timer: call update_timer every 1 second self.timer_event = Clock.schedule_interval(self.update_timer, 1) self.ids.status_label.text = "Timer started!" def update_timer(self, dt): # Update your timer logic here (e.g., increment count) self.timer_count += 1 self.ids.status_label.text = f"Timer: {self.timer_count}s"
Why You Saw That "bound method..." Text
When you did str(self.btn) (or whatever method/control you used), you're converting the method object itself to a string, not the value it returns or the control's content. Methods are objects in Python, so their string representation shows where they're bound to (your MainScreen instance). Always make sure you either:
- Call the method (add parentheses:
self.get_username()) to get its return value, or - Access the control's property (like
self.ids.username_input.text) to get the user's input.
内容的提问来源于stack exchange,提问作者Dawid Archibald

