亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

解決在 Python 中使用 GUI 從插入值生成自定義數字的問題

解決在 Python 中使用 GUI 從插入值生成自定義數字的問題

慕的地6264312 2022-12-14 20:36:30
你好我有 python 學習項目。我想在 GUI 中插入兩個數字,它們定義程序從中生成隨機數的范圍。我確實在按下按鈕調用函數時遇到問題。并不斷出現錯誤 ValueError: invalid literal for int() with base 10: '',當嘗試將字符串從 GUI 中的條目轉換為 int,然后將它們插入 random.randint 時。感謝幫助!from tkinter import *import randomroot = Tk()root.title("Generator custom random number")#function that gets number from entry converts string to int and defines target number in stevilodef function():    string_answer1 = prvo_stevilo.get()    int1 = int(string_answer1)    string_answer2 = drugo_stevilo.get()    int2 = int(string_answer2)    stevilo = random.randint(int1, int2)#Defining GUInavodilo = Label(root, text="Choose custom lower and upper number to chose random number from", width=60)navodilo2 = Label(root, text="From", width=20, borderwidth=3)navodilo3 = Label(root, text="To", width=20, borderwidth=3)prvo_stevilo = Entry(root, width=20, borderwidth=3)drugo_stevilo = Entry(root, width=20, borderwidth=3)gumb = Button(root, text="Generate number", width=17, height=2, command=function)praznavrstica = Label(root, text="")izpis = Label(root, text="Random number is: ", width=20)izpis_stevila = Label(root, text="" + stevilo)#Showcase of guinavodilo.grid(row=0, column=0, columnspan=1)navodilo2.grid(row=1, column=0, columnspan=1)navodilo3.grid(row=3, column=0, columnspan=1)prvo_stevilo.grid(row=2, column=0, columnspan=1)drugo_stevilo.grid(row=4, column=0, columnspan=1)praznavrstica.grid(row=5, column=0, columnspan=1)gumb.grid(row=6, column=0, columnspan=1)praznavrstica.grid(row=7, column=0, columnspan=1)izpis.grid(row=8, column=0, columnspan=1)izpis_stevila.grid(row=9, column=0, columnspan=1)#Looproot.mainloop()
查看完整描述

1 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

我注意到您的代碼幾乎沒有問題。我能夠在沒有太多更改的情況下運行它,盡管這可能不是最好的方法。


首先回答您的問題:您收到此錯誤,因為您正試圖將字符串 -> '' 更改為 int。發生這種情況是因為 function() 在您單擊按鈕之前正在運行。


另一個問題:


izpis_stevila = Label(root, text="" + stevilo)

在調用 function() 之前,變量 'stevilo' 根本不存在,所以從這里刪除它。


我的改變建議:


1)


gumb = Button(root, text="Generate number", width=17, height=2,command = lambda: function())

如果沒有 lambda,無論按鈕的狀態如何,您的函數都會運行。


2)


first = IntVar(root, value=0)

second = IntVar(root, value=1)

prvo_stevilo = Entry(root, width=20, borderwidth=3, textvariable=first)

drugo_stevilo = Entry(root, width=20, borderwidth=3, textvariable=second)

如果您在輸入中沒有任何值的情況下運行函數,您將收到錯誤消息。此更改允許您為條目小部件設置默認值。


3)


def function():

if prvo_stevilo.get() == '' or drugo_stevilo.get() =='':

    return

else:

    string_answer1 = prvo_stevilo.get()

    int1 = int(string_answer1)

    string_answer2 = drugo_stevilo.get()

    int2 = int(string_answer2)

    stevilo = random.randint(int1, int2)

    izpis_stevila = Label(root, text=str(stevilo))

    izpis_stevila.grid(row=9, column=0)

首先檢查您的條目是否為空。其次更新標簽,還記得在此處將 int 更改為字符串 text=str(stevilo)。


查看完整回答
反對 回復 2022-12-14
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號