第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

慕的地6264312 2022-12-14 20:36:30
你好我有 python 學(xué)習(xí)項目。我想在 GUI 中插入兩個數(shù)字,它們定義程序從中生成隨機數(shù)的范圍。我確實在按下按鈕調(diào)用函數(shù)時遇到問題。并不斷出現(xiàn)錯誤 ValueError: invalid literal for int() with base 10: '',當(dāng)嘗試將字符串從 GUI 中的條目轉(zhuǎn)換為 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條經(jīng)驗 獲得超6個贊

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


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


另一個問題:


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

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


我的改變建議:


1)


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

如果沒有 lambda,無論按鈕的狀態(tài)如何,您的函數(shù)都會運行。


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)

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


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


查看完整回答
反對 回復(fù) 2022-12-14
  • 1 回答
  • 0 關(guān)注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號