简体   繁体   中英

Showing variable in a pop-up window

I have a small 2D game and I want to print a message when it's finished. The code is in Python and I used both Pygame and Tkinter for 2D graphic. When the game is finished it shows the message below:

messagebox.showinfo('Finish','Mouse ate all cheese slices.\nPolicies saved to file.')

I have a variable called steps and I need to show it in that window but it raise an error when I tried it like this:

messagebox.showinfo('Finish','Mouse ate all cheese slices after {steps}.\nPolicies saved to file.')

Update: Using f-string solved the problem:

messagebox.showinfo('Finish',f'Mouse ate all cheese slices after {steps}.\nPolicies saved to file.')

You need to use an f string. Your code should look something like this:

messagebox.showinfo('Finish',f'Mouse ate all cheese slices after {steps}.\nPolicies saved to file.')

Here's a link to learn more ways to use this sort of formatting: Python F-Strings

Try to use an f-string:

messagebox.showinfo('Finish',f'Mouse ate all cheese slices after {steps}.\nPolicies saved to file.')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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