a=self.plant.currentText() print(a) index = col_list_g1.index(a) print("该城市在列表中的索引为:", index) print("对应的温度为", col_list_g2[index])
时间: 2024-04-07 15:31:26 浏览: 5
这段代码中,首先获取下拉框中当前选中的文本,并将其赋值给变量a,然后使用index函数查找a在列表col_list_g1中第一次出现的位置,并将返回的下标值赋值给变量index。接着打印出a在列表中的位置和对应的温度值,即col_list_g2[index]。需要注意的是,如果a不在列表col_list_g1中,会抛出ValueError异常,需要做好异常处理。
相关问题
from PyQt5.QtWidgets import QApplication, QPushButton from PyQt5 import uic from dao.datab import dataInfo import pymysql class Stats: def __init__(self): # 从文件中加载UI定义 # 从 UI 定义中动态 创建一个相应的窗口对象 # 注意:里面的控件对象也成为窗口对象的属性了 # 比如 self.ui.button , self.ui.textEdit super().__init__() self.ui = uic.loadUi("Form - untitled.ui") #print(self.ui.__dict__) hostaddr=self.ui.hostaddr.text() username=self.ui.username.text() password=self.ui.password.text() database=self.ui.database.text() tablename=self.ui.tablename.text() # 信号和槽 self.ui.login.clicked.connect(self.handleCalc) def handleCalc(self): print(hostaddr) # dataInfo.connect(hostaddr=self.hostaddr, username=self.username, password=self.password, database=self.database, # table=self.tablename) con = pymysql.connect(host=hostaddr, password=password, port=3306, user=username, charset='utf8', database=database, table=tablename) cur = con.cursor() statement = "select * from danxuanti where id=1" data = cur.execute(statement) print(data) app = QApplication([]) stats = Stats() stats.ui.show() app.exec_()给这段代码改错
这段代码有一些问题,我来给您进行修改:
```python
from PyQt5.QtWidgets import QApplication
from PyQt5.uic import loadUi
import pymysql
class Stats:
def __init__(self):
# 从文件中加载UI定义
# 从 UI 定义中动态创建一个相应的窗口对象
# 注意:里面的控件对象也成为窗口对象的属性了
# 比如 self.ui.button , self.ui.textEdit
self.ui = loadUi("Form - untitled.ui")
# 信号和槽
self.ui.login.clicked.connect(self.handleCalc)
def handleCalc(self):
hostaddr = self.ui.hostaddr.text()
username = self.ui.username.text()
password = self.ui.password.text()
database = self.ui.database.text()
tablename = self.ui.tablename.text()
# 连接数据库
con = pymysql.connect(host=hostaddr,
user=username,
password=password,
database=database,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cur = con.cursor()
statement = "select * from {table} where id=1".format(table=tablename)
cur.execute(statement)
data = cur.fetchone()
print(data)
con.close()
if __name__ == '__main__':
app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()
```
修改的内容如下:
1. `from PyQt5.QtWidgets import QApplication, QPushButton`这一行代码中多了一个`QPushButton`,但在代码中并没有使用到,因此可以删除。
2. `from PyQt5 import uic`改为`from PyQt5.uic import loadUi`,因为`uic`模块中只有`loadUi`函数是我们需要用到的。
3. `from dao.datab import dataInfo`这一行代码不需要,可以删除。
4. 在`__init__`方法中,`super().__init__()`这一行代码并没有什么作用,可以删除。
5. 在`handleCalc`方法中,需要修改连接数据库的代码。原来的代码中使用了一个`dataInfo`对象来进行数据库连接,但在代码中并没有定义这个对象,因此需要改为使用`pymysql`模块来进行连接。此外,还需要注意设置`charset`参数为`utf8mb4`,避免出现中文乱码问题,同时设置`cursorclass`参数为`pymysql.cursors.DictCursor`,可以使查询结果以字典的形式返回,方便后续的数据处理。
6. `con.close()`要在`cur.fetchone()`语句执行后关闭,避免出现异常。
7. 在`if __name__ == '__main__':`语句块中,`app.exec()`应改为`app.exec_()`。
这样修改后,应该可以正常运行了。
约瑟夫环改错class Node: def __init__(self,data): self.data=data self.next=Noneclass linklist: def __init__(self): self.head=None self.data=None def isEmpty(self): if self.head: return False else: return True def length(self): if self.isEmpty(): return 0 else: t = self.head n = 1 while t.next: if t.next == self.head: break t = t.next n = n + 1 return n def addhead(self,data): node = Node(data) if self.isEmpty(): self.head = node self.tail = self.head else: node.next = self.head self.head = node self.tail.next = self.head def addtail(self,data): node=Node(data) if self.isEmpty(): self.addhead(data) else: t=self.head n=1 l=self.length() while n<l: n=n+1 t=t.next t.next=node node.next=self.head self.tail=node def delete(self,index): if self.isEmpty(): print("The linked list is empty") else: t = self.head l = self.length() if index == 0: self.head = t.next self.tail.next = self.head elif index == l - 1: n = 1 while n < l - 1: t = t.next n = n + 1 t.next = self.head self.tail = t elif index > l - 1: print("Out of range") elif index < 0: print("Wrong operation") else: n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next.next t.next = a def insert(self,data,index): l = self.length() if index == 0 or self.isEmpty(): self.addhead(data) elif index >= l: self.addtail(data) else: node = Node(data) t = self.head n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next t.next = node node.next = a def search(self,a): t=self.head for i in range(a): t=t.next return t.data def form(self,datalist): self.addhead(datalist[0]) for i in range(1,len(datalist)): self.addtail(datalist[i]) t = self.head while t.next != self.head: t = t.nextn,p=map(int,input().split(' '))data=[]p=p-1for i in range(1,n+1): data.append(i)print(data)datalist=[]for i in range(len(data)): datalist.append(int(data[i]))link=linklist()link.form(datalist)a=pb=[]while link.length()>0: b.append(link.search(a)) link.delete(a) a=a+p while a>=link.length(): a=a-link.length()print(b)
改错如下:
```python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkList:
def __init__(self):
self.head = None
self.tail = None
def isEmpty(self):
if self.head:
return False
else:
return True
def length(self):
if self.isEmpty():
return 0
else:
t = self.head
n = 1
while t.next:
if t.next == self.head:
break
t = t.next
n += 1
return n
def addhead(self, data):
node = Node(data)
if self.isEmpty():
self.head = node
self.tail = self.head
else:
node.next = self.head
self.head = node
self.tail.next = self.head
def addtail(self, data):
node = Node(data)
if self.isEmpty():
self.addhead(data)
else:
t = self.head
n = 1
l = self.length()
while n < l:
n += 1
t = t.next
t.next = node
node.next = self.head
self.tail = node
def delete(self, index):
if self.isEmpty():
print("The linked list is empty")
else:
t = self.head
l = self.length()
if index == 0:
self.head = t.next
self.tail.next = self.head
elif index == l - 1:
n = 1
while n < l - 1:
t = t.next
n += 1
t.next = self.head
self.tail = t
elif index > l - 1:
print("Out of range")
elif index < 0:
print("Wrong operation")
else:
n = 1
while n < index:
t = t.next
n += 1
a = t.next.next
t.next = a
def insert(self, data, index):
l = self.length()
if index == 0 or self.isEmpty():
self.addhead(data)
elif index >= l:
self.addtail(data)
else:
node = Node(data)
t = self.head
n = 1
while n < index:
t = t.next
n += 1
a = t.next
t.next = node
node.next = a
def search(self, a):
t = self.head
for i in range(a):
t = t.next
return t.data
def form(self, datalist):
self.addhead(datalist[0])
for i in range(1, len(datalist)):
self.addtail(datalist[i])
t = self.head
while t.next != self.head:
t = t.next
n, p = map(int, input().split(' '))
data = [i for i in range(1, n+1)]
print(data)
datalist = []
for i in range(len(data)):
datalist.append(data[i])
link = LinkList()
link.form(datalist)
a = p-1
b = []
while link.length() > 0:
b.append(link.search(a))
link.delete(a)
a += p-1
while a >= link.length():
a -= link.length()
print(b)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)